简体   繁体   中英

How can this sql grouped query be corrected? (I think this is sql generic, we use postgresql)

Data set example is as follows:

noteid seq notebalance
1       4      125.00
1       3      120.00
2       8      235.00
2       6      235.00
2       5      200.00
3       9      145.00
4       17     550.00
4       16     550.00
4       14     500.00
4       12     450.00
4       10     400.00
...

so we basically have the latest notebalance at the beginning of each noteid group.

What is the proper sql syntax to obtain the latest balances for each noteid?

as in:

1       4      125.00
2       8      235.00
3       9      145.00
4       17     550.00

A generic (= ANSI SQL) solution would be to use a window function:

select noteid, seq, notebalance
from (
   select noteid, seq, notebalance,
          row_number() over (partition by noteid order by seq desc) as rn
   from the_table
) t
where rn = 1
order by noteid;

When using Postgres, it's usually faster to use the distinct on operator:

select distinct on (noteid) noteid, seq, notebalance
from the_table
order by noteid, seq desc;

SQLFiddle example: http://sqlfiddle.com/#!15/8ca27/2

I think ROW_NUMBER() is what you are looking for. This is similar to this SO question .

"the record with the highest seq" := "there is no record with a higher seq (for this noteid )"

SELECT noteid, seq, notebalance
FROM the_table tt
WHERE NOT EXISTS (
  SELECT * FROM the_table nx
  WHERE nx.noteid = tt.noteid
  AND nc.seq > tt.seq
  )
ORDER BY noteid
 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM