Search code examples
postgresqljoingroup-byunionrow-number

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


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: table content

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: expected result

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: current result

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)???


Solution

  • 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.