質問編集履歴
4
わかりやすくやりたい内容の追加を見やすくしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -198,7 +198,21 @@
|
|
198
198
|
|
199
199
|
|
200
200
|
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
201
|
-
【やりたい内容の追加と現状】3/15 18:15
|
215
|
+
【やりたい内容の追加⓶と現状】3/15 18:15
|
202
216
|
|
203
217
|
|
204
218
|
|
3
やりたい内容の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -189,3 +189,119 @@
|
|
189
189
|
)step2
|
190
190
|
|
191
191
|
group by shisaku_id, shisaku_name,new_or_existing
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
【やりたい内容の追加と現状】3/15 18:15
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
・やりたい内容
|
206
|
+
|
207
|
+
・キャンペーン三ヵ月前までのポイント利用履歴から新規/既存のフラグを作り、その各々の数を抽出 →前回までの内容
|
208
|
+
|
209
|
+
・その新規/既存の数の中で、キャンペーン期間中にポイント利用があったかた(新規/既存のそれぞれにて)の数の抽出 →追記した内容
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
〈完成イメージ〉施策ID/施策名/新規,既存フラグ/新規,既存の数/(新規,既存の各々で)施策期間中のポイント消費者数
|
214
|
+
|
215
|
+
〈仕様〉
|
216
|
+
|
217
|
+
・キャンペーン期間:2018/1/26~2018/2/9
|
218
|
+
|
219
|
+
・ポイントの消費があった方:
|
220
|
+
|
221
|
+
fuyo_pt_tj、kan_pt_tj、kan_pt_kgの3種類の内、いずれかのカラムに数値が入っている(0も含める)方
|
222
|
+
|
223
|
+
〈考えていること〉
|
224
|
+
|
225
|
+
・with句とcount文の間に更にSelect文を書き、サブクエリを増やす。
|
226
|
+
|
227
|
+
・with句の中に、キャンペーン開始日と終了日の期間を定義し、条件内で利用(前回、with句内で定義したキャンペーンスタート日は、キャンペーン前日を定義した方が都合がよかったため、1/25をStartPreとして修正致しました)
|
228
|
+
|
229
|
+
といった方向で記述を試みているのですが、
|
230
|
+
|
231
|
+
"前回頂いたフラグを活かしたまま、さらにその条件の中でキャンペーン期間にポイント消費があった"という表現がわからず、教えて頂けないでしょうか。
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
〈追記作成を行ったクエリ〉*現状はエラーになります
|
238
|
+
|
239
|
+
with param as (
|
240
|
+
|
241
|
+
select '2018-01-25':: date campaign_StartPre, '2018-02-07':: date oubo_date_limit ,'2018-01-26':: date campaign_Start,'2018-02-09':: date campaign_End
|
242
|
+
|
243
|
+
)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
select shisaku_id, shisaku_name, new_or_existing, new_or_existing_count , shohi_flg, count(*) as shohi_flg
|
248
|
+
|
249
|
+
from (
|
250
|
+
|
251
|
+
select *
|
252
|
+
|
253
|
+
,case when
|
254
|
+
|
255
|
+
exists(
|
256
|
+
|
257
|
+
select 1 from point_rireki2
|
258
|
+
|
259
|
+
where kihon_id = cmpgn.kihon_id
|
260
|
+
|
261
|
+
and "date" between param.campaign_Start and param.campaign_End
|
262
|
+
|
263
|
+
and (fuyo_pt_tj is not null or kan_pt_tj is not null or kan_pt_kg is not null)
|
264
|
+
|
265
|
+
)
|
266
|
+
|
267
|
+
then '消費' else '消費なし' end as shohi_flg
|
268
|
+
|
269
|
+
from campaign cmpgn cross join param
|
270
|
+
|
271
|
+
where cmpgn.oubo_date < param.oubo_date_limit
|
272
|
+
|
273
|
+
) step1
|
274
|
+
|
275
|
+
from(
|
276
|
+
|
277
|
+
select shisaku_id, shisaku_name, new_or_existing, count(*) as new_or_existing_count
|
278
|
+
|
279
|
+
from (
|
280
|
+
|
281
|
+
select *
|
282
|
+
|
283
|
+
, case when
|
284
|
+
|
285
|
+
exists(
|
286
|
+
|
287
|
+
select 1 from point_rireki2
|
288
|
+
|
289
|
+
where kihon_id = cmpgn.kihon_id
|
290
|
+
|
291
|
+
and "date" between param.campaign_StartPre - '3 months'::interval and param.campaign_StartPre
|
292
|
+
|
293
|
+
and (fuyo_pt_tj is not null or kan_pt_tj is not null)
|
294
|
+
|
295
|
+
)
|
296
|
+
|
297
|
+
then '既存' else '新規' end as new_or_existing
|
298
|
+
|
299
|
+
from campaign cmpgn cross join param
|
300
|
+
|
301
|
+
where cmpgn.oubo_date < param.oubo_date_limit
|
302
|
+
|
303
|
+
) step2
|
304
|
+
|
305
|
+
)step3
|
306
|
+
|
307
|
+
group by shisaku_id, shisaku_name, new_or_existing,shohi_flg
|
2
実現したい内容を修正致しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
【やりたいこと】
|
1
|
+
【やりたいこと】*修正しました
|
2
2
|
|
3
3
|
下記のような2つのテーブルから、
|
4
4
|
|
@@ -13,6 +13,26 @@
|
|
13
13
|
(=ポイント履歴のテーブルにおいて、応募日から3カ月以内のポイントA,Bがともに(全て)0またはNULL⇒新規、 ポイントが入っている⇒既存)
|
14
14
|
|
15
15
|
で区別したいと思っております。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
↓
|
22
|
+
|
23
|
+
《やりたいことの修正》
|
24
|
+
|
25
|
+
・キャンペーン利用者の新規/既存の数を把握したい
|
26
|
+
|
27
|
+
・新規/既存の判定については、
|
28
|
+
|
29
|
+
キャンペーン期間の3カ月前までの期間(2017/10/25~2018/1/25)において、ポイント利用履歴があるかないかです。
|
30
|
+
|
31
|
+
*ポイント履歴内に"0"でもデータがあれば、'既存'の扱いとしたいです。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
16
36
|
|
17
37
|
|
18
38
|
|
@@ -128,4 +148,44 @@
|
|
128
148
|
|
129
149
|
【現状】
|
130
150
|
|
151
|
+
下記で記述しているのですが、
|
152
|
+
|
153
|
+
point_A,point_Bが、データとして0が入っている場合、"既存"としたいのですが"新規"となってしまい、
|
154
|
+
|
155
|
+
困っています。
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
select shisaku_id, shisaku_name, new_or_existing,count(*) as new_or_existing_count
|
164
|
+
|
165
|
+
from(
|
166
|
+
|
167
|
+
select *, case when abs(exist_point) > 0 then '既存' else '新規' end as new_or_existing
|
168
|
+
|
169
|
+
from (
|
170
|
+
|
171
|
+
select *
|
172
|
+
|
173
|
+
,(select sum(coalesce(point_A,0)+coalesce(point_B,0))
|
174
|
+
|
131
|
-
|
175
|
+
from point_rireki2
|
176
|
+
|
177
|
+
where kihon_id=cmpgn.kihon_id and "date" between '2017-10-25' and '2018-01-25'
|
178
|
+
|
179
|
+
) as exist_point
|
180
|
+
|
181
|
+
from campaign cmpgn
|
182
|
+
|
183
|
+
where oubo_date < '2018-02-07'
|
184
|
+
|
185
|
+
--oubo_date < '2018-02-07' が施策実行条件により絞り込み
|
186
|
+
|
187
|
+
) step1
|
188
|
+
|
189
|
+
)step2
|
190
|
+
|
191
|
+
group by shisaku_id, shisaku_name,new_or_existing
|
1
テーブル内容の更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,6 +34,70 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
+
〔point_rireki Table〕
|
38
|
+
|
39
|
+
CREATE TABLE point_rireki
|
40
|
+
|
41
|
+
("date" timestamp, "user_id" int, "point_A" int, "point_B" int, "old" int, "area" varchar(3))
|
42
|
+
|
43
|
+
;
|
44
|
+
|
45
|
+
INSERT INTO point_rireki
|
46
|
+
|
47
|
+
("date", "user_id", "point_A", "point_B", "old", "area")
|
48
|
+
|
49
|
+
VALUES
|
50
|
+
|
51
|
+
('2017-10-01 00:00:00', 1001, 10, 200, 10, '東京'),
|
52
|
+
|
53
|
+
('2017-10-07 00:00:00', 1005, 100, 50, 22, '神奈川'),
|
54
|
+
|
55
|
+
('2017-10-10 00:00:00', 1010, 99, 10, 30, '東京'),
|
56
|
+
|
57
|
+
('2017-10-01 00:00:00', 1001, 10, 200, 10, '東京'),
|
58
|
+
|
59
|
+
('2017-10-22 00:00:00', 1015, 190, 20, 40, '千葉'),
|
60
|
+
|
61
|
+
('2017-10-11 00:00:00', 1010, 120, 90, 30, '東京'),
|
62
|
+
|
63
|
+
('2017-10-27 00:00:00', 1010, 170, 80, 30, '東京'),
|
64
|
+
|
65
|
+
('2017-10-28 00:00:00', 1015, 40, 280, 40, '千葉')
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
〔campaign Table〕
|
70
|
+
|
71
|
+
CREATE TABLE campaign
|
72
|
+
|
73
|
+
("apply_id" int, "shisaku_id" varchar(6), "shisaku_name" varchar(3), "user_id" int, "apply_date" timestamp)
|
74
|
+
|
75
|
+
;
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
INSERT INTO campaign
|
80
|
+
|
81
|
+
("apply_id", "shisaku_id", "shisaku_name", "user_id", "apply_date")
|
82
|
+
|
83
|
+
VALUES
|
84
|
+
|
85
|
+
(10001, 'AYC100', 'C施策', 1001, '2017-10-01 00:00:00'),
|
86
|
+
|
87
|
+
(10002, 'AYC100', 'C施策', 1005, '2017-10-07 00:00:00'),
|
88
|
+
|
89
|
+
(10003, 'AYC100', 'C施策', 1010, '2017-10-10 00:00:00'),
|
90
|
+
|
91
|
+
(10004, 'AYB100', 'B施策', 1015, '2017-10-22 00:00:00'),
|
92
|
+
|
93
|
+
(10005, 'AYB100', 'B施策', 1010, '2017-10-11 00:00:00'),
|
94
|
+
|
95
|
+
(10006, 'AYC100', 'C施策', 1001, '2017-10-25 00:00:00')
|
96
|
+
|
97
|
+
;
|
98
|
+
|
99
|
+
|
100
|
+
|
37
101
|
|
38
102
|
|
39
103
|
【実行環境】
|