在 GROUP BY 查询之前获取最新条目

发布于 2024-11-09 04:04:38 字数 807 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2024-11-16 04:04:38

避免自连接的另一种选择(我总是讨厌自连接)

SELECT package_name,price,date FROM
(
 SELECT package_name,price,date,
        ROW_NUMBER() OVER (PARTITION BY package_name ORDER BY date DESC) as rw
 FROM products
) a
WHERE rw=1;

Another option that avoids the self join (I always hate self joins)

SELECT package_name,price,date FROM
(
 SELECT package_name,price,date,
        ROW_NUMBER() OVER (PARTITION BY package_name ORDER BY date DESC) as rw
 FROM products
) a
WHERE rw=1;
桃扇骨 2024-11-16 04:04:38
SELECT t.package_name, p.price, t.MaxDate
    FROM (SELECT package_name, MAX(date) AS MaxDate
              FROM products
              GROUP BY package_name) t
        INNER JOIN products p
            ON t.package_name = p.package_name
                and t.MaxDate = p.date
SELECT t.package_name, p.price, t.MaxDate
    FROM (SELECT package_name, MAX(date) AS MaxDate
              FROM products
              GROUP BY package_name) t
        INNER JOIN products p
            ON t.package_name = p.package_name
                and t.MaxDate = p.date
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文