2017-11-21 13 views
-1

は、クエリによってグループがthe linkで利用可能なファイルに示されている方法は、結果を表示するために使用することができる方法グループ最大(ケース)及び(ケース)状況で

select 
max(case when label in (value_1) then value else null) as "value_a" 
    (case when label in (value_2) then value else null) as "value_b" 
    (case when label in (value_3) then value else null) as "value_c" 
    (case when label in (value_4) then value else null) as "value_d" 
max(case when label in (value_5) then value else null) as "value_e" 
from table; 

が含まれるのでしょうか?

+3

:一つの方法は、row_number()使用することですグループ化された式は例外ではありません。また、サンプルデータと期待される出力を含む[MCVE]を提供してください。 – MT0

+0

申し訳ありませんが、あなたの疑似コードクエリが何をすべきか分かりません。あなたはテーブルにz = 'a'、z = 'b'などが存在するかどうかを検出したいと思うようですが、なぜ時々それは 'max'と時には無しですか?これはどういう意味ですか? –

+1

質問を編集し、サンプルデータと希望する結果を提供してください。質問は意味をなさない。 –

答えて

0

私が正しく理解している場合は、各列に値の一覧が必要です。クエリは `引き上げるよう集計関数を使用していないあなたが選択された用語と` value_b`、 `value_c`と` value_d`行の間のすべてのカンマが欠落している

with t as (
     select t.*, 
      dense_rank() over (order by a desc) as seqnum_a, 
      dense_rank() over (order by b desc) as seqnum_b, 
      dense_rank() over (order by c desc) as seqnum_c, 
      dense_rank() over (order by d desc) as seqnum_d, 
      dense_rank() over (order by e desc) as seqnum_e 
     from t 
    ) 
select seqnum, max(a) as a, max(b) as b, max(c) as c, max(d) as d, max(e) as e 
from ((select distinct seqnum_a as seqnum, a, NULL as b, NULL as c, NULL as d, NULL as e 
     from t 
    ) union all 
     (select distinct seqnum_b, NULL, b, NULL as c, NULL as d, NULL as e 
     from t 
    ) union all 
     (select distinct seqnum_c, NULL, NULL as b, c, NULL as d, NULL as e 
     from t 
    ) union all 
     (select distinct seqnum_d, NULL, NULL as b, NULL as c, d, NULL as e 
     from t 
    ) union all 
     (select distinct seqnum_e, NULL as a, NULL as b, NULL as c, NULL as d, e 
     from t 
    ) 
    ) abcde 
group by seqnum 
order by seqnum;