PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Menghasilkan peringkat urutan terurut pada kueri kompleks

Bagaimana kalau menggandakan row_number()?

with enchilada as (
SELECT firstname,lastname,personid,year,(SELECT COALESCE(SUM(thevalue),0)
FROM assets WHERE personidref = w.personid) AS assets ,
(SELECT COALESCE(SUM(amount),0) FROM liabilities WHERE personidref = w.personid AND year = w.year) AS liabilities,
((SELECT COALESCE(SUM(thevalue),0) FROM assets WHERE personidref = w.personid AND year = w.year) - (SELECT COALESCE(SUM(amount),0) FROM liabilities WHERE personidref = w.personid AND year = w.year)) as worth,
row_number() over(ORDER BY w.worth) as rank
FROM members w
WHERE year = 2012
ORDER BY worth DESC LIMIT 2 )
select row_number() over (order by rank) as new_rank, * from enchilada;


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Lewati argumen baris perintah ke sql (Postgres)

  2. LPAD() Fungsi di PostgreSQL

  3. Panggilan untuk makalah untuk PGDay.IT 2011 telah diperpanjang

  4. Postgresql lambat dihapus dari mana ada

  5. org.hibernate.type.TextType dan Oracle