sql - 选择列上的 MAX,然后从依赖于第一个值的列中选择 MAX

标签 sql sql-server sql-server-2005 max

我有这样的表:

CREATE TABLE #Test
(
    ParentID int,
    DateCreated DATETIME,
    ItemNo int
)

INSERT INTO #Test(ParentID, DateCreated, ItemNo) VALUES  (1,'2008-10-01 00:00:00.000',0)
INSERT INTO #Test(ParentID, DateCreated, ItemNo) VALUES  (1,'2008-10-01 00:00:00.000',1)
INSERT INTO #Test(ParentID, DateCreated, ItemNo) VALUES  (1,'2008-05-01 00:00:00.000',2)
INSERT INTO #Test(ParentID, DateCreated, ItemNo) VALUES  (1,'2008-05-01 00:00:00.000',3)

INSERT INTO #Test(ParentID, DateCreated, ItemNo) VALUES  (2,'2008-06-01 00:00:00.000',3)
INSERT INTO #Test(ParentID, DateCreated, ItemNo) VALUES  (2,'2008-06-01 00:00:00.000',4)
INSERT INTO #Test(ParentID, DateCreated, ItemNo) VALUES  (2,'2008-04-01 00:00:00.000',6)
INSERT INTO #Test(ParentID, DateCreated, ItemNo) VALUES  (2,'2008-04-01 00:00:00.000',8)

我需要一种在相同的parentID上选择最高的DateCreated和最高的ItemNo的方法,并且是否可以在查询中使用像这样的解决方案:

SELECT * 
FROM #Test t
  JOIN
  (
    If I could get maximum row here somehow that would be great
  ) maxt
  ON t.ParentID = maxt.ParentID 
  JOIN SomeOtherTable sot
  ON sot.DateCreated = maxt.MaxDateCreated
  AND sot.ItemNo = maxt.MaxItemNo
GROUP BY
  sot.Something

只是为了澄清结果应该是什么样子:

ParentID        DateCreated           ItemNo   ParentID      MaxDateCreated    MaxItemNo
  1,       '2008-10-01 00:00:00.000'  ,0         1,      '2008-10-01 00:00:00.000',1
  1,       '2008-10-01 00:00:00.000'  ,1         1,      '2008-10-01 00:00:00.000',1
  1,       '2008-05-01 00:00:00.000'  ,2         1,      '2008-10-01 00:00:00.000',1
  1,       '2008-05-01 00:00:00.000'  ,3         1,      '2008-10-01 00:00:00.000',1
  2,       '2008-06-01 00:00:00.000'  ,3         2,      '2008-06-01 00:00:00.000',4
  2,       '2008-06-01 00:00:00.000'  ,4         2,      '2008-06-01 00:00:00.000',4
  2,       '2008-04-01 00:00:00.000'  ,6         2,      '2008-06-01 00:00:00.000',4
  2,       '2008-04-01 00:00:00.000'  ,8         2,      '2008-06-01 00:00:00.000',4

最佳答案

如果您需要 DateCreated 的最大值以及此 DateCreated 的最大 ItemNo:

select ParentId,
       DateCreated as MaxDateCreated,
       ItemNo as MaxItemNo
     from 
      (select PArentID,DateCreated,ItemNo, 
         Row_Number() OVER (PARTITION BY ParentID 
                             ORDER BY DateCreated DESC, 
                                      ItemNo Desc) as RN
         from #Test          
      ) t3
     where RN=1

SQLFillde demo

更新

要获得问题中提到的结果,您应该将其与 #TEST 一起加入,例如:

SELECT * 
FROM Test t
  JOIN
  (
select ParentId,
           DateCreated as MaxDateCreated,
           ItemNo as MaxItemNo
         from 
          (select PArentID,DateCreated,ItemNo, 
             Row_Number() OVER (PARTITION BY ParentID 
                                 ORDER BY DateCreated DESC, 
                                          ItemNo Desc) as RN
             from test          
          ) t3
         where RN=1
  ) maxt
  ON t.ParentID = maxt.ParentID 

SQLFiddle demo

关于sql - 选择列上的 MAX,然后从依赖于第一个值的列中选择 MAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18123172/

相关文章:

sql - 选择第二个最新日期的条目

sql-server - 将具有多个内部连接的 sql 查询转换为 LINQ

sql-server - SQL Server Web 版和 Express 版之间有性能差异吗?

sql-server - 生成db/sprocs/etc的sql脚本。通过命令行

sql - 在sql server中使用自引用

sql - 在多个变量上使用 RANK 的快速帮助

SQL 2000/2005/2008 - 查找列的唯一约束名称

sql - 不设置标识自动增加主键

sql - 在 SQL 中查找组中的连续数字

sql - 如何在 SQL 中生成通向给定节点的层次结构路径?