-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmp.sql
448 lines (401 loc) · 9.37 KB
/
tmp.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
DELETE FROM tmps;
INSERT INTO tmps (order_id, customer_id, sa, create_date, customer_create_date, rank)
SELECT
order_id,
customer_id,
sa,
create_date,
customer_create_date,
cast(
CASE WHEN create_date > now - sa THEN 1
WHEN create_date > now - 2 * sa THEN 2
ELSE 3
END as integer
) rank
FROM (
SELECT
order_id,
customer_id,
now,
(now - customer_create_date) / 3 sa,
create_date,
customer_create_date
FROM (
SELECT
order_id,
dtb_order.customer_id customer_id,
cast(extract(epoch FROM date(dtb_order.create_date)) as integer) create_date,
cast(extract(epoch FROM date(dtb_customer.create_date)) as integer) customer_create_date,
cast(extract(epoch FROM date(NOW())) as integer) now
FROM dtb_customer JOIN
dtb_order ON (dtb_order.customer_id = dtb_customer.customer_id)
WHERE
dtb_customer.del_flg = 0 AND dtb_order.del_flg = 0 AND dtb_order.status = 5
) tmp
) tmp
;
//-------------------------------------------------------------------------------------------------------------
ALTER TABLE "public"."tmps" ALTER COLUMN "rank" TYPE integer USING rank::integer;
SELECT extract(epoch FROM date('1999-10-10')) - extract(epoch FROM date('1989-02-05'));
SELECT extract(epoch FROM age(date('1999-10-10'), timestamp '1989-02-05'));
SELECT age(date('1999-10-10'), timestamp '1989-02-05') * 2;
//-------------------------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS tmps2;
create table tmps2 (
order_id integer NOT NULL,
customer_id integer NOT NULL,
create_date timestamp without time zone NOT NULL,
customer_create_date timestamp without time zone NOT NULL,
sa interval,
order_rank integer
);
CREATE INDEX tmps2_order_id_customer_id ON tmps2 USING btree (order_id, customer_id);
//最初データ取る
DELETE FROM tmps2;
INSERT INTO tmps2 (order_id, customer_id, create_date, customer_create_date)
SELECT
order_id,
customer_id,
create_date,
customer_create_date
FROM (
SELECT
order_id,
dtb_order.customer_id customer_id,
dtb_order.create_date create_date,
dtb_customer.create_date customer_create_date
FROM dtb_customer JOIN
dtb_order ON (dtb_order.customer_id = dtb_customer.customer_id)
WHERE
dtb_customer.del_flg = 0 AND dtb_order.del_flg = 0 AND dtb_order.status = 5
) tmp
//基準日によりデータ計算
UPDATE tmps2 SET sa = age(now(), customer_create_date) / 3 , order_rank =
CASE WHEN create_date > now() - age(now(), customer_create_date)/3 THEN 1
WHEN create_date > now() - age(now(), customer_create_date)/3 * 2 THEN 2
ELSE 3
END
SELECT dtb_customer.customer_id,
cast (tmp1.count as float ) / cast (tmp2.count as float ) a,
cast (tmp2.count as float ) / cast (tmp3.count as float ) b
FROM
dtb_customer
LEFT JOIN
(SELECT
COUNT(order_id) count,
max(customer_id) customer_id
FROM tmps2 WHERE order_rank = 1 GROUP BY customer_id) as tmp1
ON (dtb_customer.customer_id = tmp1.customer_id)
LEFT JOIN
(SELECT
COUNT(order_id) count,
max(customer_id) customer_id
FROM tmps2 WHERE order_rank = 2 GROUP BY customer_id) as tmp2
ON (dtb_customer.customer_id = tmp2.customer_id)
LEFT JOIN
(SELECT
COUNT(order_id) count,
max(customer_id) customer_id
FROM tmps2 WHERE order_rank = 3 GROUP BY customer_id) as tmp3
ON (dtb_customer.customer_id = tmp3.customer_id)
ORDER BY dtb_customer.customer_id
SELECT
dtb_customer.customer_id,
tmp1.count coun1,
tmp2.count count2,
tmp3.count count3
FROM
dtb_customer
LEFT JOIN
(SELECT
COUNT(order_id) count,
max(customer_id) customer_id
FROM tmps2 WHERE order_rank = 1 GROUP BY customer_id) as tmp1
ON (dtb_customer.customer_id = tmp1.customer_id)
LEFT JOIN
(SELECT
COUNT(order_id) count,
max(customer_id) customer_id
FROM tmps2 WHERE order_rank = 2 GROUP BY customer_id) as tmp2
ON (dtb_customer.customer_id = tmp2.customer_id)
LEFT JOIN
(SELECT
COUNT(order_id) count,
max(customer_id) customer_id
FROM tmps2 WHERE order_rank = 3 GROUP BY customer_id) as tmp3
ON (dtb_customer.customer_id = tmp3.customer_id)
本物に近い
-------------------------------
DROP TABLE IF EXISTS tmps3;
create table tmps3 (
order_id integer,
customer_id integer,
birth timestamp without time zone,
sex smallint,
payment_total integer,
customer_create_date timestamp without time zone ,
order_create_date timestamp without time zone
);
CREATE INDEX tmps3_order_id_customer_id ON tmps3 USING btree (order_id, customer_id);
--最初データ
DELETE FROM tmps3;
INSERT INTO tmps3 (order_id, customer_id, birth, sex, payment_total, customer_create_date, order_create_date)
SELECT
order_id,
dtb_customer.customer_id customer_id,
dtb_customer.birth birth,
dtb_customer.sex sex,
payment_total,
dtb_customer.create_date customer_create_date,
dtb_order.create_date order_create_date
FROM
dtb_customer FULL OUTER JOIN dtb_order ON (dtb_customer.customer_id = dtb_order.customer_id)
WHERE
(dtb_customer.del_flg = 0 OR dtb_customer.del_flg IS NULL)
AND
(dtb_order.del_flg = 0 OR dtb_order.del_flg IS NULL)
;
--ランク分ける処理
--パターン1のみランク判定を行う 基準日からランクデータを集計する
--2015-05-01 23:59:59~2014-05-01 23:59:59
--2014-05-01 23:59:59~2013-05-01 23:59:59
--2013-05-01 23:59:59~2012-05-01 23:59:59
SELECT
customer_id,
CASE WHEN A IS NULL THEN 0 ELSE A END,
CASE WHEN B IS NULL THEN 0 ELSE B END,
CASE WHEN C IS NULL THEN 0 ELSE C END,
CASE
WHEN
A IS NULL AND B IS NULL AND C IS NULL
THEN
'休眠'
WHEN
a1 > 1 AND a2> 1
THEN
'最優良'
WHEN
a1 > 1 AND a2 = 0
THEN
'優良'
WHEN
a1 > 1 AND a2 < -1
THEN
'準優良'
WHEN
a1 = 0 AND a2 > 1
THEN
'優良傾向'
WHEN
a1 = 0 AND a2 = 0
THEN
'安定'
WHEN
a1 = 0 AND a2 < -1
THEN
'休眠傾向'
WHEN
a1 < -1 AND a2 > 1
THEN
'休眠予備A'
WHEN
a1 < -1 AND a2 = 0
THEN
'休眠予備B'
WHEN
a1 < -1 AND a2 < -1
THEN
'休眠'
END as p
FROM
(
SELECT
t.customer_id customer_id,
CASE
WHEN B IS NULL AND A IS NULL THEN 0
WHEN B IS NULL THEN 100
WHEN A IS NULL THEN -100
ELSE cast(cast (A as float ) / cast (B as float ) * 100 - 100 as integer)
END a1,
CASE
WHEN C IS NULL AND B IS NULL THEN 0
WHEN C IS NULL THEN 100
WHEN B IS NULL THEN -100
ELSE cast(cast (B as float ) / cast (C as float ) * 100 - 100 as integer)
END a2,
A,
B,
C
FROM
dtb_customer t
LEFT JOIN
(SELECT
COUNT(order_id) A, customer_id
FROM
tmps3
WHERE
order_create_date <= date('2015-05-01 23:59:59') AND order_create_date > date('2014-05-01 23:59:59') AND
customer_create_date >= date('2012-05-01 23:59:59') AND customer_id IS NOT null AND order_id IS NOT null
GROUP BY customer_id) t1
USING (customer_id)
LEFT JOIN
(SELECT
COUNT(order_id) B, customer_id
FROM
tmps3
WHERE
order_create_date <= date('2014-05-01 23:59:59') AND order_create_date > date('2013-05-01 23:59:59') AND
customer_create_date >= date('2012-05-01 23:59:59') AND customer_id IS NOT null AND order_id IS NOT null
GROUP BY customer_id) t2
USING (customer_id)
LEFT JOIN
(SELECT
COUNT(order_id) C, customer_id
FROM
tmps3
WHERE
order_create_date <= date('2013-05-01 23:59:59') AND order_create_date > date('2012-05-01 23:59:59') AND
customer_create_date >= date('2012-05-01 23:59:59') AND customer_id IS NOT null AND order_id IS NOT null
GROUP BY customer_id) t3
USING (customer_id)
WHERE t.del_flg = 0
) tmp ORDER BY customer_id ;
-----------------テストデータ
INSERT INTO dtb_order VALUE (
order_id
,order_temp_id
,customer_id
,message
,order_name01
,order_name02
,order_kana01
,order_kana02
,order_email
,order_tel01
,order_tel02
,order_tel03
,order_fax01
,order_fax02
,order_fax03
,order_zip01
,order_zip02
,order_pref
,order_addr01
,order_addr02
,order_sex
,order_birth
,order_job
,subtotal
,discount
,deliv_id
,deliv_fee
,charge
,use_point
,add_point
,birth_point
,tax
,total
,payment_total
,payment_id
,payment_method
,note
,status
,create_date
,update_date
,commit_date
,payment_date
,device_type_id
,del_flg
,memo01
,memo02
,memo03
,memo04
,memo05
,memo06
,memo07
,memo08
,memo09
,memo10
,order_tax_rate
,order_tax_rule
,order_zipcode
,order_country_id
,plg_coupon_manage_coupon_code
,receipt_flg
,plg_volume_discount
,plg_manage_discount
,plg_coupon_manage_coupon_discount
,order_company_name
,sale_subtotal
,user_agent
,customer_rast_point
)
SELECT
order_id + (SELECT max(order_id) FROM dtb_order) order_id
,order_temp_id
,customer_id
,message
,order_name01
,order_name02
,order_kana01
,order_kana02
,order_email
,order_tel01
,order_tel02
,order_tel03
,order_fax01
,order_fax02
,order_fax03
,order_zip01
,order_zip02
,order_pref
,order_addr01
,order_addr02
,order_sex
,order_birth
,order_job
,subtotal
,discount
,deliv_id
,deliv_fee
,charge
,use_point
,add_point
,birth_point
,tax
,total
,payment_total
,payment_id
,payment_method
,note
,status
,create_date
,update_date
,commit_date
,payment_date
,device_type_id
,del_flg
,memo01
,memo02
,memo03
,memo04
,memo05
,memo06
,memo07
,memo08
,memo09
,memo10
,order_tax_rate
,order_tax_rule
,order_zipcode
,order_country_id
,plg_coupon_manage_coupon_code
,receipt_flg
,plg_volume_discount
,plg_manage_discount
,plg_coupon_manage_coupon_discount
,order_company_name
,sale_subtotal
,user_agent
,customer_rast_point
FROM dtb_order