Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to use qualify in postgresql

Tags:

sql

postgresql

the below query works in teradata, is there a way to write the same query in postgresql? I am getting error while running this ppostgresql 10

select *
from
    product
qualify
    row_number() over (partition by product_key order by product_no) = 1; 
like image 455
neoo Avatar asked Jan 21 '26 04:01

neoo


2 Answers

Postgres has distinct on, which should be even more performant:

select distinct on (product_key) p.*
from product p
order by product_key, product_no; 

This is usually the best method in Postgres to get one row per group.

like image 176
Gordon Linoff Avatar answered Jan 23 '26 19:01

Gordon Linoff


You need subquery :

select p.*
from (select p.*,
             row_number() over (partition by product_key order by product_no) as seq
      from product p
     ) p 
where seq = 1;
like image 21
Yogesh Sharma Avatar answered Jan 23 '26 21:01

Yogesh Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!