Я хочу получить ранг / row_number для каждого клиента, но сохранить ранг таким же, если последующие транзакции будут сделаны в течение 24 часов.
Вот что я пытался сделать:
with x as (
select 214142446 as unique_customer_id, 19331177 as transaction_id, to_timestamp('26/04/2017 11:28:01', 'DD/MM/YYYY HH24:MI:SS') as log_date union all
select 214142446, 1193324450, to_timestamp('26/04/2017 12:29:50', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1193327391, to_timestamp('26/04/2017 12:42:17', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1194753492, to_timestamp('03/05/2017 14:56:33', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1198679862, to_timestamp('23/05/2017 11:11:46', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1198893043, to_timestamp('24/05/2017 11:23:33', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1198948574, to_timestamp('24/05/2017 16:12:35', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1198952915, to_timestamp('24/05/2017 17:04:10', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1200182526, to_timestamp('30/05/2017 14:23:43', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1202206887, to_timestamp('07/06/2017 14:19:33', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1202163318, to_timestamp('08/06/2017 13:04:01', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1203934169, to_timestamp('14/06/2017 17:08:58', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1205508321, to_timestamp('20/06/2017 13:05:00', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1205541102, to_timestamp('20/06/2017 14:35:41', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1205562693, to_timestamp('20/06/2017 15:58:01', 'DD/MM/YYYY HH24:MI:SS') union all
select 214142446, 1206273894, to_timestamp('23/06/2017 10:18:45', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1231603750, to_timestamp('06/10/2017 13:47:20', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1231697368, to_timestamp('07/10/2017 12:34:08', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1231699273, to_timestamp('07/10/2017 12:46:01', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1231699882, to_timestamp('07/10/2017 12:52:28', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1231975996, to_timestamp('09/10/2017 20:06:47', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232073502, to_timestamp('10/10/2017 17:05:06', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232074795, to_timestamp('10/10/2017 17:15:16', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232250513, to_timestamp('12/10/2017 09:15:41', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232251114, to_timestamp('12/10/2017 09:21:43', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232371321, to_timestamp('13/10/2017 11:50:36', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232391230, to_timestamp('13/10/2017 15:07:00', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232392844, to_timestamp('13/10/2017 15:23:44', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232450858, to_timestamp('14/10/2017 08:43:02', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232451164, to_timestamp('14/10/2017 08:51:17', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232755473, to_timestamp('17/10/2017 09:11:38', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 1232766588, to_timestamp('17/10/2017 11:15:38', 'DD/MM/YYYY HH24:MI:SS') union all
select 2115686902, 232989319, to_timestamp('19/10/2017 13:42:25', 'DD/MM/YYYY HH24:MI:SS')
)
select *
, sum (case when hour_diff > 24 then 1 else 0 end)
over (partition by unique_customer_id order by log_date rows unbounded preceding) + 1 as customer_trxn_cnt
from (
select *
, datediff(hour, lag(log_date, 1) over (partition by unique_customer_id order by log_date)::timestamp, log_date::timestamp) as hour_diff
from x
)
order by unique_customer_id, log_date
;
Проблема с этим заключается в том, что он учитывает предыдущую транзакцию log_date при определении того, сохранять или нет ранг одинаковым. Я ищу выходные данные для групповых транзакций, выполненных в течение 24 часов друг от друга, и присваиваю им одинаковый ранг:
unique_customer_id|booking_id|to_timestamp |customer_trxn_cnt|
------------------|----------|-------------------|-----------------|
4142446| 19331177|2017-04-26 12:28:01| 1|
4142446| 19332445|2017-04-26 13:29:50| 1|
4142446| 19332739|2017-04-26 13:42:17| 1|
4142446| 19475349|2017-05-03 15:56:33| 2|
4142446| 19867986|2017-05-23 12:11:46| 3|
4142446| 19889304|2017-05-24 12:23:33| 4|
4142446| 19894857|2017-05-24 17:12:35| 4|
4142446| 19895291|2017-05-24 18:04:10| 4|
4142446| 20018252|2017-05-30 15:23:43| 5|
4142446| 20220688|2017-06-07 15:19:33| 6|
4142446| 20216331|2017-06-08 14:04:01| 6|
4142446| 20393416|2017-06-14 18:08:58| 7|
4142446| 20550832|2017-06-20 14:05:00| 8|
4142446| 20554110|2017-06-20 15:35:41| 8|
4142446| 20556269|2017-06-20 16:58:01| 8|
4142446| 20627389|2017-06-23 11:18:45| 9|
15686902| 23160370|2017-10-06 14:47:20| 1|
15686902| 23169738|2017-10-07 13:34:08| 1|
15686902| 23169923|2017-10-07 13:46:01| 1|
15686902| 23169982|2017-10-07 13:52:28| 1|
15686902| 23197596|2017-10-09 21:06:47| 2|
15686902| 23207352|2017-10-10 18:05:06| 2|
15686902| 23207475|2017-10-10 18:15:16| 3|
15686902| 23225053|2017-10-12 10:15:41| 4|
15686902| 23225114|2017-10-12 10:21:43| 4|
15686902| 23237131|2017-10-13 12:50:36| 5|
15686902| 23239120|2017-10-13 16:07:00| 5|
15686902| 23239284|2017-10-13 16:23:44| 5|
15686902| 23245088|2017-10-14 09:43:02| 6|
15686902| 23245114|2017-10-14 09:51:17| 6|
15686902| 23275543|2017-10-17 10:11:38| 7|
15686902| 23276658|2017-10-17 12:15:38| 7|
15686902| 23298931|2017-10-19 14:42:25| 8|
Подумал о суммировании сброса hour_diff при достижении 24 и затем row_number()
против этого? Любая помощь будет высоко ценится.