teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

chousei

2019/08/28 06:12

投稿

yambejp
yambejp

スコア117944

answer CHANGED
@@ -129,4 +129,38 @@
129
129
  |2019-07-01|1|2|
130
130
  |2019-08-01|1|2|
131
131
  |2019-09-01|1|1|
132
- |2019-10-01|1|1|
132
+ |2019-10-01|1|1|
133
+
134
+ # カレンダーを利用
135
+ ```SQL
136
+ create table user_info_table(id int primary key,UserInsertDate date,UpdateDate date,RegistrationCode int);
137
+ insert into user_info_table values
138
+ (1,'20190401','20191001',1),
139
+ (2,'20190301','20190801',1),
140
+ (3,'20190401','20190601',2);
141
+
142
+ /*とりあえず1年分投入してますが、使い回すので100年分くらい投入しておくとよい*/
143
+ create table nengetu(d date unique);
144
+ insert into nengetu values
145
+ ('20190101'),
146
+ ('20190201'),
147
+ ('20190301'),
148
+ ('20190401'),
149
+ ('20190501'),
150
+ ('20190601'),
151
+ ('20190701'),
152
+ ('20190801'),
153
+ ('20190901'),
154
+ ('20191001'),
155
+ ('20191101'),
156
+ ('20191201');
157
+ ```
158
+
159
+ - 集計
160
+ ```SQL
161
+ select d,RegistrationCode,count(*) as cnt from user_info_table as t1
162
+ inner join nengetu as t2
163
+ on t2.d between t1.UserInsertDate and t1.UpdateDate
164
+ group by d,RegistrationCode
165
+ order by d,RegistrationCode;
166
+ ```

2

procedure

2019/08/28 06:12

投稿

yambejp
yambejp

スコア117944

answer CHANGED
@@ -24,4 +24,109 @@
24
24
  |10月|1|1|
25
25
 
26
26
  という結果を発生させるのですか?
27
- カウントが1の理由がわかりません
27
+ カウントが1の理由がわかりません
28
+
29
+ # procedure
30
+ この手の処理はprocedureを使ってテンポラリーテーブルを作って処理します
31
+
32
+ - 元データ
33
+ ```SQL
34
+ create table user_info_table(id int primary key,UserInsertDate date,UpdateDate date,RegistrationCode int);
35
+ insert into user_info_table values
36
+ (1,'20190401','20191001',1),
37
+ (2,'20190301','20190801',1),
38
+ (3,'20190401','20190601',2);
39
+ ```
40
+
41
+ - procedure作成
42
+ 1回だけ以下を実行
43
+ ```SQL
44
+ create table user_info_table(id int primary key,UserInsertDate date,UpdateDate date,RegistrationCode int);
45
+ insert into user_info_table values
46
+ (1,'20190401','20191001',1),
47
+ (2,'20190301','20190801',1),
48
+ (3,'20190401','20190601',2);
49
+ ;
50
+
51
+ drop procedure if exists proc_test;
52
+ delimiter //
53
+ create procedure proc_test()
54
+ begin
55
+ declare a int;
56
+ declare b date;
57
+ declare c date;
58
+ declare d int;
59
+ declare done int default 0;
60
+ declare cur cursor for
61
+ select id,UserInsertDate,UpdateDate,RegistrationCode from user_info_table;
62
+ declare continue handler for sqlstate '02000' set done = 1;
63
+ set @sql="";
64
+ open cur;
65
+ repeat
66
+ fetch cur into a,b,c,d;
67
+ if not done then
68
+ set @date=b;
69
+ while @date<=c do
70
+ if @sql="" then
71
+ create temporary table tmp(nengetu date,RegistrationCode int);
72
+ set @sql="insert into tmp ";
73
+ else
74
+ set @sql=concat(@sql,"union all ");
75
+ end if;
76
+ set @sql=concat(@sql,"select '",@date,"',",d," ");
77
+ set @date=@date + interval 1 month;
78
+ end while;
79
+ end if;
80
+ until done end repeat;
81
+ close cur;
82
+ PREPARE stmt from @sql;
83
+ EXECUTE stmt;
84
+ end
85
+ //
86
+ delimiter ;
87
+ ```
88
+ - テスト
89
+ ```SQL
90
+ call proc_test;
91
+ select * from tmp;
92
+ ```
93
+ |nengetu|RegistrationCode|
94
+ |:--|--:|
95
+ |2019-04-01|1|
96
+ |2019-05-01|1|
97
+ |2019-06-01|1|
98
+ |2019-07-01|1|
99
+ |2019-08-01|1|
100
+ |2019-09-01|1|
101
+ |2019-10-01|1|
102
+ |2019-03-01|1|
103
+ |2019-04-01|1|
104
+ |2019-05-01|1|
105
+ |2019-06-01|1|
106
+ |2019-07-01|1|
107
+ |2019-08-01|1|
108
+ |2019-04-01|2|
109
+ |2019-05-01|2|
110
+ |2019-06-01|2|
111
+
112
+
113
+ - 集計
114
+ ```SQL
115
+ call proc_test;
116
+ select nengetu,RegistrationCode,count(*) as cnt from tmp
117
+ group by nengetu,RegistrationCode;
118
+
119
+ ```
120
+ |nengetu|RegistrationCode|cnt|
121
+ |:--|--:|--:|
122
+ |2019-03-01|1|1|
123
+ |2019-04-01|1|2|
124
+ |2019-04-01|2|1|
125
+ |2019-05-01|1|2|
126
+ |2019-05-01|2|1|
127
+ |2019-06-01|1|2|
128
+ |2019-06-01|2|1|
129
+ |2019-07-01|1|2|
130
+ |2019-08-01|1|2|
131
+ |2019-09-01|1|1|
132
+ |2019-10-01|1|1|

1

tuiki

2019/08/28 02:15

投稿

yambejp
yambejp

スコア117944

answer CHANGED
@@ -2,4 +2,26 @@
2
2
  ダミーで100年分くらいデータを作っておき、joinすればよいでしょう
3
3
 
4
4
  具体的には命題の情報では回答が難しいので
5
- サンプルをcreate table+insert形式で例示ください
5
+ サンプルをcreate table+insert形式で例示ください
6
+
7
+ # 追記
8
+
9
+ ```SQL
10
+ create table user_info_table(id int primary key,UserInsertDate date,UpdateDate date,RegistrationCode int);
11
+ insert into user_info_table values
12
+ (1,'20190401','20191001',1);
13
+ ```
14
+ というレコードが1つしかないテーブルから
15
+
16
+ |年月|RegistrationCode|カウント|
17
+ |--:|--:|--:|
18
+ |4月|1|1|
19
+ |5月|1|1|
20
+ |6月|1|1|
21
+ |7月|1|1|
22
+ |8月|1|1|
23
+ |9月|1|1|
24
+ |10月|1|1|
25
+
26
+ という結果を発生させるのですか?
27
+ カウントが1の理由がわかりません