Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择列上的最大值,然后选择依赖于第一个值的列中的最大值_Sql_Sql Server_Sql Server 2005_Max - Fatal编程技术网

Sql 选择列上的最大值,然后选择依赖于第一个值的列中的最大值

Sql 选择列上的最大值,然后选择依赖于第一个值的列中的最大值,sql,sql-server,sql-server-2005,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-

我有这样的桌子:

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)
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上使用最高ItemNo创建的最高日期,如果可以在查询中使用如下解决方案:

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)
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的最大日期和最大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

UPD

为了得到问题中提到的结果,你应该加入#测试,比如:

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 

什么是MaxItemNo,为什么不是3和8而不是1和4?@t-clausen.dk MaxItemNo是最大ItemNo,如果日期在同一个参数上是最大的,则父1需要的组合是“2008-10-01 00:00.000”,1,因为这是ParentID的最大创建日期中的最大ItemNo1@t-clausen.dk因为我需要DateCreated和ItemNo的最高组合,因为DateCreated具有第一优先级,所以“2008-10-01 00:00:00.000”,1>“2008-05-01 00:00:00.000”,3我只看到表中的两行result@t-clausen.dk要获得所需的结果,您应该使用#Test on ParentId连接此表。我已经更新了我的答案。