简体   繁体   中英

How to calculate the 2/3 purchased by month in Postgresql?

I have one table named 'sales'.

create table sales
    (
        cust    varchar(20),
        prod    varchar(20),
        day integer,
        month   integer,
        year    integer,
        state   char(2),
        quant   integer
    );
insert into sales values ('Bloom', 'Pepsi', 2, 12, 2001, 'NY', 4232);
insert into sales values ('Knuth', 'Bread', 23, 5, 2005, 'PA', 4167);
insert into sales values ('Emily', 'Pepsi', 22, 1, 2006, 'CT', 4404);
insert into sales values ('Emily', 'Fruits', 11, 1, 2000, 'NJ', 4369);
insert into sales values ('Helen', 'Milk', 7, 11, 2006, 'CT', 210);
insert into sales values ('Emily', 'Soap', 2, 4, 2002, 'CT', 2549);
insert into sales values ('Bloom', 'Eggs', 30, 11, 2000, 'NJ', 559);

.... There are 498 rows in total. Here is the overview of this table:

在此处输入图像描述

Now I want to get the the month by which time, 2/3 of the sales quantities have been purchased for each combination of customer and product.

The result should look like this:

在此处输入图像描述

Anyone has the idea to help me with this?

You could use window function, then aggregation:

select year, cust, prod, min(month)
from (
    select 
        s.*,
        sum(quant) over(partition by year, cust, prod order by month, day) quant_cumul,
        sum(quant) over(partition by year, cust, prod) quant_total
    from sales s
) t
where quant_cumul > 2/3 * quant_total
group by year, cust, prod

The inner query computes a cumulative sum and a global sum of quantities for each (year, cust, prod) tupe. Then, the outer query filters on the records where the cumulative sum exceeds the 2/3 of the total quantity, and gets the minimum month for each tuple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM