0

フィールドに値「N」、「D」、または「V」が含まれるように、テーブルの値を分析して生成した SQL のビューがあります。行ごとではなく、列ごとに合計を計算できます...これは可能ですか?

例:

データ

No, Col_1, Col_2, Col_3

 1,     N,     N,     N

 2,     N,     D,     D

 3,     N,     V,     D

 4,     V,     V,     V

行 3 には 1N、1V、および 3ds があり、行 4 には 4V があるとどのように要約しますか?

賭けは非常に単純ですが、悲しいことに私もそうです!

前もって感謝します、ピーター

4

2 に答える 2

0
 select case when col_1 = 'N' then 1 else 0 end as n_count from tablename;

それを一般化する:

 select 
   case when col_1 = 'N' then 1 else 0 end 
   + case when col_2 = 'N' then 1 else 0 end 
   + case when col_2 = 'N' then 1 else 0 end as n_count,
   case when col_1 = 'V' then 1 else 0 end 
   + case when col_2 = 'V' then 1 else 0 end 
   + case when col_2 = 'V' then 1 else 0 end as v_count,
   ....
  from tablename;
于 2009-10-19T22:28:13.803 に答える
0

どうですか?

select no,
sum(case when val = 'N' then 1 else 0 end) ncnt,
sum(case when val = 'V' then 1 else 0 end) vcnt,
sum(case when val = 'D' then 1 else 0 end) dcnt from
(select no, col_1 val from t union all 
 select no, col_2 from t union all
 select no, col_3 from t)
group by no
order by no
于 2009-10-19T22:51:28.177 に答える