0

我有一张包含日期和时间列的表格。我需要获取唯一ID的最小日期及其时间(col c)和最大日期及其时间(col D)。

例如

     A    B                C      D
     1   12/10/2012      8.00     11.00
     1   2/10/2013      10.00    12.00         
     2   1/10/2013      1.00     2.00
     2   9/10/2012      6.00     7.00

结果

     1  12/10/2012   8.00    2/10/2013  12.00
     2  9/10/2012    6.00    1/10/2013   2.00
4

1 回答 1

3
WITH CTE AS 
(
SELECT A, MIN(B) as MinB, MAX(B) as MaxB
FROM TableName
GROUP BY A
)
SELECT C.A, C.MinB, tMin.C as MinC, C.MaxB, tMax.D as MaxD
FROM CTE C
LEFT JOIN TableName tMin on tMin.A = C.A AND tMin.B = c.MinB
LEFT JOIN TableName tMax on tMax.A = C.A AND tMax.B = c.MaxB
于 2013-06-19T06:17:49.137 回答