5

我有三个表,如下所示:

在此处输入图像描述

我正在尝试获得如下输出:

在此处输入图像描述

这是我到目前为止所尝试的

    SELECT table1.tb1_a,
CASE WHEN table3.tb3_a IS NOT NULL THEN
    tb3_b
    ELSE 'No city for him yet'
END AS 'City'
FROM table1
LEFT OUTER JOIN table2 ON
    table1.tb1_a = table2.tb2_a
LEFT OUTER JOIN table3 ON
    table2.tb2_a = table3.tb3_a
WHERE table3.tb3_a IN
    (

    )

现在我正在努力研究如何选择 tb3_a 列的最大值

4

5 回答 5

2

这应该做你需要的:

SELECT t1.tb1_a, COALESCE(t3.tb3_a, 'No city for him yet') AS City
FROM table1 t1
LEFT JOIN (
    SELECT MAX(tb2_b) AS tb2_b, tb2_a
    FROM table2
    GROUP BY tb2_a
) t2 ON (t2.tb2_a = t1.tb1_a)
LEFT JOIN table3 t3 ON (t3.tb3_a = t3.tb2_b);

关键点是中间的内联视图,我们在其中创建了一种虚拟表,其中包含tb2_b每个的最大值tb2_a。然后我们可以加入这个来达到预期的结果。

于 2013-02-21T17:30:19.487 回答
1

像这样的东西应该工作

select tb1_a, nvl(max(city), 'no city for him yet') thecity
from etc
group by tbl_a
于 2013-02-21T17:21:11.063 回答
1

我想你想用分析函数来做到这一点。方法如下:

SELECT tb1_a, coalesce(tb3_b, 'No city for him yet') as City
from (select table1.tb1_a, tb3_b,
             ROW_NUMBER() over (partition by table1.tbl1_a order by tb3_a desc) as seqnum
      FROM table1 LEFT OUTER JOIN
           table2
           ON table1.tb1_a = table2.tb2_a LEFT OUTER JOIN
           table3
           ON table2.tb2_a = table3.tb3_a
     ) t
where seqnum = 1

这用于row_number()确定 table3 中的最后一个条目。这是由where seqnum = 1子句选择的。

于 2013-02-21T17:22:10.927 回答
1

这有效:

http://sqlfiddle.com/#!4/7ba1c/12

SELECT table1.tb1_a,
       CASE WHEN table3.tb3_a IS NOT NULL THEN tb3_b
            ELSE 'No city for him yet'
       END AS City,
table2.*, table3.*
FROM table1
LEFT OUTER JOIN table2 ON    table1.tb1_a = table2.tb2_a
LEFT OUTER JOIN table3 ON    table2.tb2_b = table3.tb3_a
where tb2_b = (select max(tb2_b) 
                 from table2 t22 
                where t22.tb2_a = tb1_a)
or    tb2_b is null
order by 1

我认为 where 子句可以解释自己

于 2013-02-21T18:38:49.187 回答
0

试试这个,希望对你有帮助

with table_1 as
 (select 10 as tb1_a, 'John' as tb1_b
    from dual
  union all
  select 20 as tb1_a, 'John1' as tb1_b
    from dual
  union all
  select 30 as tb1_a, 'John2' as tb1_b from dual),
table_2 as
 (select 10 as tb2_a, 100 as tb2_b
    from dual
  union all
  select 10 as tb2_a, 1000 as tb2_b
    from dual
  union all
  select 10 as tb2_a, 10000 as tb2_b
    from dual
  union all
  select 20 as tb2_a, 200 as tb2_b
    from dual
  union all
  select 20 as tb2_a, 2000 as tb2_b
    from dual
  union all
  select 20 as tb2_a, 20000 as tb2_b from dual),
table_3 as
 (select 100 as tb3_a, 'City1' as tb3_b
    from dual
  union all
  select 1000 as tb3_a, 'City10' as tb3_b
    from dual
  union all
  select 10000 as tb3_a, 'City100' as tb3_b
    from dual
  union all
  select 200 as tb3_a, 'City2' as tb3_b
    from dual
  union all
  select 2000 as tb3_a, 'City20' as tb3_b
    from dual
  union all
  select 20000 as tb3_a, 'City200' as tb3_b from dual)

select user_id, city
  from (select u.tb1_a as user_id,
               nvl(c.tb3_b, 'No city') as city,
               rank() over(partition by u.tb1_a order by tb3_a) as city_rank
          from table_1 u, table_2 u_c, table_3 c
         where u.tb1_a = u_c.tb2_a(+)
           and u_c.tb2_b = c.tb3_a(+)) t1
 where city_rank = 1
于 2013-02-21T17:28:41.743 回答