【发布时间】:2023-03-27 10:58:01
【问题描述】:
我有一张这样的桌子:
ID | Cost | Month | Year |
-------------------------------------
1081| 13000 | 5 | 2017 |
1081| 13500 | 9 | 2016 |
1081| 21000 | 2 | 2016 |
1229| 6500 | 7 | 2017 |
1229| 7800 | 5 | 2016 |
1312| 110000 | 8 | 2017 |
1312| 120000 | 5 | 2017 |
1312| 99000 | 5 | 2016 |
我试过了:
select id, year, month, avg(cost) as Avg Cost
from price_history
group by id, year, month
order by id, year desc, month desc
我怎样才能像这样显示最新数据:
ID | Cost | Month | Year |
-------------------------------------
1081| 13000 | 5 | 2017 |
1229| 6500 | 7 | 2017 |
1312| 110000 | 8 | 2017 |
【问题讨论】:
-
我从您的输出示例中无法理解 - 您需要 AVG 成本还是什么?
-
我只想要基于月份和年份的最新数据/行。
标签: sql postgresql greatest-n-per-group odoo-8