5

以下に示すような3つのテーブルがあります。

ここに画像の説明を入力

そして、私は以下のような出力を得ようとしています:

ここに画像の説明を入力

これが私が今まで試したことです

    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

これは機能します:

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 に答える
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 に答える
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 に答える