简体   繁体   中英

How remove rows or filter rows with some identical columns values by some criteria?

I have a table which I filter and sort with this query

 select * from XXXX where Segment='Gewerbe' and Division='Strom' and consumption >= 1000 and consumption < 2000 and rank<= 5 and market_id='39a2e05fd43300c998558ef56bca18e2' order by consumption ,rank 

The result set contains basically three groups of results which are grouped MARKET_ID and RANK . Each subresult differs by RANK (1..N).

The difficult part: I am interested only in the subresults with the highest RANK In this case I need each row with RANK=5 . So I want to eliminate the rows with RANK=1..4 . Note that highest RANK for each subresult might be smaller than 5.

Result table

I think the following would work too:

select market_id, consumption, max(costs_netto), max(rank) from XXX where Segment='Gewerbe' and Division='Strom' and consumption >= 1000 and consumption < 2000 and rank<= 5 and market_id='39a2e05fd43300c998558ef56bca18e2'[enter image description here][1]  group by market_id, consumption  order by market_id, consumption

Result with grouping

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