如何找到表中包含最大值的记录?

发布于 2024-07-10 03:05:26 字数 779 浏览 11 评论 0原文

虽然这个问题看起来很简单,但其实有点棘手。

考虑下表:

CREATE TABLE A (
  id INT,
  value FLOAT,
  "date" DATETIME,
  group VARCHAR(50) 
);

我想获取包含按 列分组的最大 日期 的记录的 ID。 换句话说,类似于“每个组的最新值是多少?”什么查询将回答该问题?

我可以获得每个组及其最大日期:

SELECT group, MAX(date) 
  FROM A 
  GROUP BY group; -- I also need the "ID" and "value"`

但我希望获得具有最高日期的记录的“ID”和值。

A 和结果之间进行 JOIN 可能是答案,但无法知道 MAX(date) 引用的是哪条记录 (如果日期重复)。

样本数据:

INSERT INTO A
  VALUES
(1, 1.0, '2000-01-01', 'A'),
(2, 2.0, '2000-01-02', 'A'),
(3, 3.0, '2000-01-01', 'B'),
(4, 2.0, '2000-01-02', 'B'),
(5, 1.0, '2000-01-02', 'B')
;

Although this question looks simple, it is kind of tricky.

Consider the following table:

CREATE TABLE A (
  id INT,
  value FLOAT,
  "date" DATETIME,
  group VARCHAR(50) 
);

I would like to obtain the ID and value of the records that contain the maximum date grouped by the column group. In other words, something like "what is the newest value for each group?" What query will answer that question?

I can get each group and its maximum date:

SELECT group, MAX(date) 
  FROM A 
  GROUP BY group; -- I also need the "ID" and "value"`

But I would like to have the "ID" and value of the record with the highest date.

Making a JOIN between A and the result could be the answer, but there is no way of knowing which record MAX(date) refers to (in case the date repeats).

Sample data:

INSERT INTO A
  VALUES
(1, 1.0, '2000-01-01', 'A'),
(2, 2.0, '2000-01-02', 'A'),
(3, 3.0, '2000-01-01', 'B'),
(4, 2.0, '2000-01-02', 'B'),
(5, 1.0, '2000-01-02', 'B')
;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

月下伊人醉 2024-07-17 03:05:27

只要“日期”列对于每个组都是唯一的,我认为这样的方法可能会起作用:

SELECT A.ID, A.Value
FROM A
  INNER JOIN (SELECT Group, MAX(Date) As MaxDate FROM A GROUP BY Group) B
    ON A.Group = B.Group AND A.Date = B.MaxDate

As long as the Date column is unique for each group I think something like this might work:

SELECT A.ID, A.Value
FROM A
  INNER JOIN (SELECT Group, MAX(Date) As MaxDate FROM A GROUP BY Group) B
    ON A.Group = B.Group AND A.Date = B.MaxDate
醉生梦死 2024-07-17 03:05:26

您可以尝试使用子查询

select group, id, value, date from A where date in
( select MAX(date) as date
  from A
  group by group )
order by group

You could try with a subquery

select group, id, value, date from A where date in
( select MAX(date) as date
  from A
  group by group )
order by group
┼── 2024-07-17 03:05:26

这正是分析函数的用途:

select group,
       id,
       value
from   (
       select group,
              id,
              value,
              date,
              max(date) over (partition by group) max_date_by_group
       from A
       )
where  date = max_date_by_group

This is just what analytic functions were made for:

select group,
       id,
       value
from   (
       select group,
              id,
              value,
              date,
              max(date) over (partition by group) max_date_by_group
       from A
       )
where  date = max_date_by_group
眼泪淡了忧伤 2024-07-17 03:05:26

如果日期是唯一的,那么您已经有了答案。 如果日期不唯一,那么您需要一些其他唯一符。 如果没有自然密钥,您的 ID 就和任何 ID 一样好。 只需在其上输入 MAX(或 MIN,无论您喜欢哪个)即可:

SELECT *
FROM A
JOIN (
    --Dedupe any non unqiue dates by getting the max id for each group that has the max date
    SELECT Group, MAX(Id) as Id
    FROM A 
    JOIN (
        --Get max date for each group
        SELECT group, MAX(date) as Date 
        FROM A 
        GROUP BY group
    ) as MaxDate ON
        A.Group = MaxDate.Group
        AND A.Date = MaxDate.Date
    GROUP BY Group
) as MaxId ON
    A.Group = MaxId.Group
    AND A.Id= MaxId.Id

If date is unique, then you already have your answer. If date is not unique, then you need some other uniqueifier. Absent a natural key, your ID is as good as any. Just put a MAX (or MIN, whichever you prefer) on it:

SELECT *
FROM A
JOIN (
    --Dedupe any non unqiue dates by getting the max id for each group that has the max date
    SELECT Group, MAX(Id) as Id
    FROM A 
    JOIN (
        --Get max date for each group
        SELECT group, MAX(date) as Date 
        FROM A 
        GROUP BY group
    ) as MaxDate ON
        A.Group = MaxDate.Group
        AND A.Date = MaxDate.Date
    GROUP BY Group
) as MaxId ON
    A.Group = MaxId.Group
    AND A.Id= MaxId.Id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文