-3

おそらく重要なクエリです。私はこのテーブルhttp://sqlfiddle.com/#!2/d0423を使用しており、mysql5.6を使用して次のテーブルを出力しようとしています。

[type][type_cnt][f_cnt_type0][f_cnt_type1][f_cnt_type2][time_00][time_00-15][time_15-30]
10        2         1             1           0           1          1          0
20        3         1             1           1           1          2          0
30        3         2             1           0           0          1          1

何か案は?

4

1 に答える 1

0
CREATE TABLE bugs (
     type varchar(2),
     flag varchar(1), 
     mins varchar(2)
 );

INSERT INTO bugs(type, flag, mins) values
('10','0','0'),
('10','1','5'),
('20','0','0'),
('20','1','5'),
('20','2','10'),
('30','0','10'),
('30','0','20'),
('30','1','40');


select 
  type, 
  count(type) as type_cnt,
  SUM(CASE WHEN flag='0' THEN 1 ELSE 0 END) as f_cnt_type0,
  SUM(CASE WHEN flag='1' THEN 1 ELSE 0 END) as f_cnt_type1,
  SUM(CASE WHEN flag='2' THEN 1 ELSE 0 END) as f_cnt_type2,
  SUM(CASE WHEN CAST(mins AS UNSIGNED) = 0 THEN 1 ELSE 0 END) as time_00,
  SUM(CASE WHEN CAST(mins AS UNSIGNED) > 0 and CAST(mins AS UNSIGNED) <= 15 THEN 1 ELSE 0 END) as time_00_15,
  SUM(CASE WHEN CAST(mins AS UNSIGNED) > 15 and CAST(mins AS UNSIGNED) <= 30 THEN 1 ELSE 0 END) as time_15_30       
from bugs
group by type

SQL:sqlfiddle

于 2013-03-17T04:28:54.493 に答える