【问题标题】:SQL Query to Get Max Value Based on Different Max Value Given Multiple RecordsSQL查询根据给定多条记录的不同最大值获取最大值
【发布时间】:2009-09-17 22:46:32
【问题描述】:

我知道我可以使用 CTE 或其他方式做到这一点,但我想知道这是否可以在一个选择查询中完成(没有子选择)。我想为 po_nbr 找到最近的 crt_ts,然后获取关联的 id。


注意:

id列不保证是连续的,我为这个问题简化了。


代码:

create table temp
(
    id int,
    po_nbr int,
    crt_ts datetime
)

insert into temp values (20, 100, '09/01/2009')
insert into temp values (3, 100, '09/03/2009')
insert into temp values (5, 100, '09/05/2009')
insert into temp values (6, 100, '09/07/2009')
insert into temp values (4, 200, '08/01/2009')
insert into temp values (29, 200, '08/03/2009')
insert into temp values (12, 200, '08/05/2009')
insert into temp values (18, 200, '08/07/2009')

想要的结果:

id  po_nbr
---------
6   100
18  200

【问题讨论】:

  • 您实际上并没有查询结果应该是什么。即便如此,托马拉克可能是对的。
  • 我给出了想要的结果……查询的输出。更新了问题以澄清几件事。
  • +1 用于包含示例表

标签: sql sql-server-2005 tsql


【解决方案1】:
SELECT
  MAX(id) id,
  po_nbr
FROM
  temp
GROUP BY
  po_nbr

要获得关联的日期,您可以这样做(注意,这意味着一个连续的 id):

SELECT
  temp.id,
  temp.po_nbr,
  temp.crt_ts
FROM
  temp
  INNER JOIN (
    SELECT MAX(id) id FROM temp GROUP BY po_nbr
  ) latest ON latest.id = temp.id

如果没有顺序 id,它将是:

SELECT
  MAX(temp.id) id,
  temp.po_nbr,
  temp.crt_ts
FROM
  temp INNER JOIN (
    SELECT   MAX(crt_ts) crt_ts, po_nbr 
    FROM     temp i
    GROUP BY po_nbr
  ) latest ON latest.crt_ts = temp.crt_ts AND latest.po_nbr = temp.po_nbr
GROUP BY
  temp.po_nbr,
  temp.crt_ts

如果保证每个po_nbr 组没有两个相同的日期,则可以省略GROUP BY

crt_tspo_nbr 上的索引有助于最后一个查询,最好创建一个组合索引。

【讨论】:

  • 不保证 ID 列是连续的。我不一定想要 max(id)...我想要与由 po_nbr 分组的 max(crt_ts) 关联的 id。
  • 使用 ROW_NUMBER(),就像我的回答一样,使用 SET SHOWPLAN_ALL ON 时,几乎一半的 TotalSubtreeCost 作为您的 GROUP BY 方法(第三次尝试)
  • 可能,但我也倾向于编写在 SQL Server 2000 上运行的 SQL,因为这就是我们这里的生产环境。 :-\
【解决方案2】:

尝试:

SELECT
    id,po_nbr --crt_ts --can show the date if you want
    FROM (SELECT 
              id,po_nbr,crt_ts
              ,ROW_NUMBER() OVER(partition BY po_nbr ORDER BY po_nbr,crt_ts DESC) AS RankValue
              FROM temp
         ) dt
    WHERE RankValue=1

【讨论】:

    【解决方案3】:
    SELECT 
      MAX(temp.id) AS id, det.po_nbr
    FROM
      temp
      INNER JOIN (SELECT po_nbr, MAX(crt_ts) AS maxcrt_ts FROM temp GROUP BY po_nbr) AS det ON temp.po_nbr = det.po_nbr AND temp.crt_ts = maxcrt_ts
    GROUP BY
      det.po_nbr
    

    【讨论】:

      【解决方案4】:
      select t.id, tm.po_nbr
      from  temp t
      inner join (
          select po_nbr, max(crt_ts) as max_ts
          from temp 
          group by po_nbr
      ) tm on t.po_nbr = tm.po_nbr and t.crt_ts = max_ts
      

      【讨论】:

        【解决方案5】:

        选择最大值(id),po_nbr 来自临时工 按 po_nbr 分组

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-02
          • 1970-01-01
          • 2013-11-05
          • 2012-03-24
          • 1970-01-01
          相关资源
          最近更新 更多