Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
SQL - How to partition for greatest values rows only?

I have a table like below:

User    Product Price   City    Month       Year
------------------------------------------------
abc     Pen         22  Mumbai  JULY        2019
abc     Pencil      21  Mumbai  AUGUST      2019
abc     Notebook    23  Mumbai  SEPTEMBER   2019
abc     Pen         22  Mumbai  OCTOBER     2019
abc     Pencil      21  Mumbai  NOVEMBER    2019
abc     Notebook    23  Mumbai  DECEMBER    2019
xyz     Pen         22  Mumbai  JULY        2019
xyz     Pencil      21  Mumbai  AUGUST      2019
xyz     Notebook    23  Mumbai  SEPTEMBER   2019
xyz     Pen         22  Mumbai  OCTOBER     2019
xyz     Pencil      21  Mumbai  NOVEMBER    2019
xyz     Notebook    23  Mumbai  DECEMBER    2019

I want to get the data from table where the price is maximum.

Expected Output:

User    Product     Price   City    Month       Year
----------------------------------------------------
abc     Notebook    23      Mumbai  SEPTEMBER   2019
abc     Notebook    23      Mumbai  DECEMBER    2019
xyz     Notebook    23      Mumbai  SEPTEMBER   2019
xyz     Notebook    23      Mumbai  DECEMBER    2019

What I tried:

Query 1:

SELECT * FROM 
(SELECT *, ROW_NUMBER() OVER (partition by "User" ORDER BY "Price" desc) as col
 FROM public.product)x
 WHERE x.col=1

Output:

User    Product     Price   City        Month       Year    col
---------------------------------------------------------------
abc     Notebook    23      Mumbai      SEPTEMBER   2019    1
xyz     Notebook    23      Mumbai      DECEMBER    2019    1

Here I am missing December month data of abc and same for xyz users

Query 2:

SELECT * FROM 
(SELECT *, ROW_NUMBER() OVER (partition by "User","Price" ORDER BY "Price" desc) as col
 FROM public.product)x
 WHERE x.col=1

Output:

User    Product     Price   City        Month       Year    col
--------------------------------------------------------------
abc     Notebook    23  Mumbai          SEPTEMBER   2019    1
abc     Pen         22  Mumbai          JULY        2019    1
abc     Pencil      21  Mumbai          AUGUST      2019    1
xyz     Notebook    23  Mumbai          DECEMBER    2019    1
xyz     Pen         22  Mumbai          JULY        2019    1
xyz     Pencil      21  Mumbai          NOVEMBER    2019    1

Here I am getting rows for every products highest price

Can someone help me to get Expected output?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can simply use a subquery and max().

SELECT p1.*
       FROM public.product p1
       WHERE p1."Price" = (SELECT max(p2."Price")
                                  FROM public.product p2);
over 4 years ago · Santiago Trujillo Report

0

Use dense_rank()

DEMO

SELECT * FROM 
(SELECT *, dense_rank() OVER (ORDER BY Price desc) as col
FROM public.product)x
WHERE x.col=1

OUTPUT:

user    product   price city    month       year    col
Xyz     Notebook    23  Mumbai  SEPTEMBER   2019    1
abc     Notebook    23  Mumbai  DECEMBER    2019    1
abc     Notebook    23  Mumbai  SEPTEMBER   2019    1
xyz     Notebook    23  Mumbai  DECEMBER    2019    1
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!