2

我有这样的表:

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
4

2 回答 2

2

如果您需要 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 演示

UPD

要获得问题中提到的结果,您应该将其与#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 演示

于 2013-08-08T10:22:12.700 回答
0
SELECT 
DateCreated, ItemNo, ParentID,
MAX(DateCreated) over (PARTITION BY ParentID)  MaxDateCreated, 
MAX(itemNo*case when rn = 1 then 1 end) over (PARTITION BY parentid) MaxItemNo
FROM
(
  SELECT DateCreated, ItemNo, ParentID, 
  row_number() over (PARTITION BY parentid order by DateCreated desc, ItemNo desc) rn 
  FROM #test
) a
于 2013-08-08T10:05:26.467 回答