Home > Software engineering >  Is it possible to find out the number of items in a row by using window functions in PostgreSQL?
Is it possible to find out the number of items in a row by using window functions in PostgreSQL?

Time:11-17

how to find the number of sellers who have payments, the time between which in a row is less than 1 minute and which are executed at least 3 times in a row? (answer is 2 sellers) and how to calculate the number of such payments? (answer is 10 payments) it seems like such a problem can be solved using window functions, but I have never encountered this kind of problem

enter image description here

CREATE TABLE T (seller_id int, payment_id varchar(3), payment_time timestamp, second_diff int);
    
INSERT INTO T (seller_id, payment_id, payment_time, second_diff)
VALUES
    (1, 'pl',  '2015-01-08 09:23:04', 151),
    (1, 'p2',  '2015-01-08 09:25:35', 50),
    (1, 'p3',  '2015-01-08 09:26:25', 48),
    (1, 'p4',  '2015-01-08 09:27:23', 36),
    (1, 'p5',  '2015-01-08 09:27:59', 41),
    (1, 'p6',  '2015-01-08 09:28:40', 70),
    (1, 'p7',  '2015-01-08 09:29:50', 50),
    (1, 'p8',  '2015-01-08 09:30:40', 45),
    (1, 'p9',  '2015-01-08 09:31:25', 35),
    (1, 'p10', '2015-01-08 09:32:00', null),
    (2, 'pll', '2015-01-08 09:25:35', 25),
    (2, 'p12', '2015-01-08 09:26:00', 55),
    (2, 'p13', '2015-01-08 09:26:55', 30),
    (2, 'p14', '2015-01-08 09:27:25', 95),
    (2, 'p15', '2015-01-08 09:29:00', null),
    (3, 'p16', '2015-01-08 10:41:00', 65),
    (3, 'p17', '2015-01-08 10:42:05', 75),
    (3, 'p18', '2015-01-08 10:43:20', 90),
    (3, 'p19', '2015-01-08 10:43:20', 39),
    (3, 'p20', '2015-01-08 10:43:59', 50),
    (3, 'p21', '2015-01-08 10:44:49', null);

CodePudding user response:

Using an aggregate function with a OVER clause containing an integer division by 60 an then filter on this division for result 0

WITH T AS
(
SELECT ... COUNT(*) OVER(PARTITION BY second_diff / 60) AS CNT, second_diff / 60 AS GRP
FROM ....
) 
SELECT * FROM T
WHERE GRP = 0

CodePudding user response:

with A as (
    select seller_id, payment_time, second_diff,
        case when
            lag(case when second_diff < 60 then 1 else 0 end)
                over (partition by seller_id order by payment_time)
              = case when second_diff < 60 then 1 else 0 end
            then 0 else 1 end as transition
    from T
), B as (
    select *,
        sum(transition)
            over (partition by seller_id order by payment_time) as grp
    from A
), C as (
    select seller_id, count(*) as p
    from B
    where second_diff < 60
    group by seller_id, grp
    having count(*) >= 3
) 
select count(distinct seller_id) as sellers, sum(p) as payments
from C;

https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=d1c98aa0a3cd6c1b8d3fdcc71ab4d7bd

  • Related