当前位置:网站首页>PgSQL queries the largest or smallest data of a field in a group

PgSQL queries the largest or smallest data of a field in a group

2022-06-24 14:16:00 GodSure0914

As shown in the figure below , A one to many situation occurs after the left connection , I want to start from this " many " Take inside price The biggest piece of data :
 Insert picture description here

terms of settlement , Add a new row to the query parameters :ROW_NUMBER() OVER(PARTITION by b.parent_charge_id ORDER BY c.price desc)idx
 Insert picture description here

explain :

1.ROW_NUMBER: This function can number the groups

2.PARTITION by : Yes parent_charge_id This field is grouped
The query result is :
 Insert picture description here
You can see that the data has been pressed id Grouped , And numbered , The number is 1 Is the maximum we want , And then the value is 1 We can get the result we want :
 Insert picture description here

原网站

版权声明
本文为[GodSure0914]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241212255868.html