下記のSQLを実行して月ごとに3回以上エントリーしたユーザ
を表示するようにしたのですがこの時、月に何回表示されたかをSQLの結果で列で表示したいのですが、重複が完全に削除されてしまい最後に3回を超えたエントリーしか表示されないのですが何がいけないのでしょうか。
select tmp.id as 'ID' , case when tmp.id_class = 1 then ‘ジョブ1’ else ‘ノーマル’ end as クラス, case tmp.month when 201611 then tmp.count else ifnull(null,'0') end as '201611', case tmp.month when 201612 then tmp.count else ifnull(null,'0') end as '201612', case tmp.month when 201701 then tmp.count else ifnull(null,'0') end as '201701' from ( select id, DATE_FORMAT(rest_date, "%Y%m") month, id_class, count(id) as count from ent.test_rest where id is not null and '2016-11-01 00:00:00' < up_date and up_date < '2017-01-31 23:59:59’ GROUP BY DATE_FORMAT(reserve_date, "%Y%m"), id HAVING count >= 3 ORDER BY id ASC ) tmp group by tmp.id
ほしい結果例
id,クラス,201611,201612,201701
a111,ジョブ1,0,3,5
a222,ノーマル,0,0,6
a333,ノーマル,14,0,3
DBのイメージ
pid id rest_date up_date
15 aaa1 2016-11-01 00:00:00 2016-11-01 00:00:00
16 aaa1 2016-11-02 00:00:00 2016-11-02 00:00:00
17 aaa1 2016-11-13 00:00:00 2016-11-13 00:00:00
18 aaa1 2016-11-16 00:00:00 2016-11-16 00:00:00
19 ccc3 2016-11-02 00:00:00 2016-11-02 00:00:00
20 ccc3 2016-11-02 00:00:00 2016-11-02 00:00:00
21 ccc3 2016-11-02 00:00:00 2016-11-02 00:00:00
22 bbb2 2016-12-01 00:00:00 2016-12-01 00:00:00
23 bbb2 2016-12-02 00:00:00 2016-12-02 00:00:00
24 bbb2 2016-12-03 00:00:00 2016-12-03 00:00:00
25 bbb2 2016-12-04 00:00:00 2016-12-04 00:00:00
26 aaa1 2017-01-09 00:00:01 2017-01-09 00:00:00
27 aaa1 2017-01-10 00:00:01 2017-01-10 00:00:00
28 aaa1 2017-01-18 00:00:01 2017-01-18 00:00:00
29 bbb2 2017-01-23 00:00:01 2017-01-23 00:00:00
30 bbb2 2017-01-24 00:00:01 2017-01-24 00:00:00
31 bbb2 2017-01-25 00:00:01 2017-01-25 00:00:00
32 bbb2 2016-12-04 00:00:00 2016-12-04 00:00:00
33 ccc3 2017-01-08 00:00:01 2017-01-08 00:00:00
34 ccc3 2017-01-08 00:00:01 2017-01-09 00:00:00
35 ccc3 2017-01-08 00:00:01 2017-01-08 00:00:00
回答2件
あなたの回答
tips
プレビュー