How can I remove the null values and make it to 10 rows in Postgresql?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 06:01:52

问题


I am new to Postgresql. I have a table called '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);

something like this:

Now I want to find the “most favorable” month (when most amount of the product was sold) and the “least favorable” month (when the least amount of the product was sold) for each product.

The result should be like this:

I entered

SELECT
    prod product,
    MAX(CASE WHEN rn2 = 1 THEN month END) MOST_FAV_MO,

    MAX(CASE WHEN rn1 = 1 THEN month END) LEAST_FAV_MO
FROM (
    SELECT
        *,
        ROW_NUMBER() OVER(PARTITION BY prod ORDER BY quant ) rn1,
        ROW_NUMBER() OVER(PARTITION BY prod ORDER BY quant DESC) rn2
    FROM sales
) x
WHERE  rn1 = 1 or rn2 = 1
GROUP BY prod,quant;

Then there are null values for each product and there are 20 rows in total:

So how can I remove the null values in these rows and make the total number of rows to 10 (There are 10 distinct products in total)???


回答1:


I would say that the GROUP BY clause should be

GROUP BY prod

Otherwise you get one line per different quant, which is not what you want.



来源:https://stackoverflow.com/questions/58278903/how-can-i-remove-the-null-values-and-make-it-to-10-rows-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!