在 GROUP BY 查询之前获取最新条目
使用 postgres,我想检索每个不同的包,但检索每个组的最新包。
例如,我有一个名为 products 的表:
package_name price date
- | 52500.0 | 20080910
- | 52900.0 | 20090422
- | 52900.0 | 20090706
ELITE PACKAGE | 62200.0 | 20080910
ELITE PACKAGE | 62500.0 | 20090706
ELITE PACKAGE | 62500.0 | 20090422
TECH PACKAGE | 57200.0 | 20080910
TECH PACKAGE | 58200.0 | 20090706
TECH PACKAGE | 58200.0 | 20090422
我想通过 SQL 查询得到以下结果:
- | 52900.0 | 20090706
ELITE PACKAGE | 62500.0 | 20090706
TECH PACKAGE | 58200.0 | 20090706
我已经尝试了一些(我认为)在 mySQL 中可以工作的方法,但我无法得到正确的结果对于 postgres。
有可能做些什么吗?我该怎么做?
谢谢,
斯蒂芬妮
Using postgres, I would like to retrieve each different packages, but retrieve the most recent package for each group.
For example, I have this table named products:
package_name price date
- | 52500.0 | 20080910
- | 52900.0 | 20090422
- | 52900.0 | 20090706
ELITE PACKAGE | 62200.0 | 20080910
ELITE PACKAGE | 62500.0 | 20090706
ELITE PACKAGE | 62500.0 | 20090422
TECH PACKAGE | 57200.0 | 20080910
TECH PACKAGE | 58200.0 | 20090706
TECH PACKAGE | 58200.0 | 20090422
And I would like to get the following result with a SQL query:
- | 52900.0 | 20090706
ELITE PACKAGE | 62500.0 | 20090706
TECH PACKAGE | 58200.0 | 20090706
I have tried a couple of things that (I think) would have worked in mySQL, but I can't get it right for postgres.
Is it something possible to do, and how would I do it?
Thanks,
Stephanie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
避免自连接的另一种选择(我总是讨厌自连接)
Another option that avoids the self join (I always hate self joins)