1
ID   Date1   T1   Date2   T2   Date3   T3   Date4   T4
1   DT11     Y    ----------NULL-----------------------  
2   DT11     N    DT22    Y    ----------NULL----------  
3   DT31     N    DT32    Y    DT33    Y    --NULL-----  
4   ----NULL---   DT42    Y    ---NULL---   DT44    Y
5   DT51     Y    ---NULL--    DT53    Y    --NULL-----  

我有一个具有上述结构的表 Table1。

要求:

  1. 检查 (T1 - T5) 中哪一个有“Y”
  2. 如果有多个“Y”,则比较日期以获取最近的日期
  3. 在具有最近日期的日期字段的名称或部分名称的新字段中设置一个值。

所以,o/p 应该是

ID  Most-recent-Date-Flag
---------------------------------------------------    
1  "Date1 or T1"  -> Since all others are null
2  "Date2 or T2"  -> T2 has 'Y'
3  "Date3 or T3"  -> one 'N' and multiple 'Y', DT33 is most recent
4  "Date4 or T4"  -> multiple 'Y', DT44 is most recent
5  "Date5 or T5"  -> multiple 'Y', DT53 is most recent

我已经设法创建查询来比较日期。如下:

    SELECT [ID]
    , MAX(UNPIVOT_DATE) AS MOST_RECENT_DATE
    FROM 
    (
    SELECT
    [ID]
    ,[Date1]
    ,[Date2]
    ,[Date3]
    ,[Date4]
    FROM TABLE1
    )SRC_TABLE
    UNPIVOT
    (
    UNPIVOT_DATE
    FOR DATES IN
    (
    [Date1]
    ,[Date2]
    ,[Date3]
    ,[Date4]
    )
    )UNPIVOT_TABLE
    GROUP BY [ID]

我被困在检查(T1-T4)字段中的“Y”并在比较后获取最近日期的列名的地方。

希望这个解释有帮助!

08/04/2013
非常感谢@MarcinJuraszek。有用!!无论如何我也可以传递字段的列名(T1-T4)吗?我尝试了以下方法,但没有奏效。

SELECT t.[ID]
,MAX(t.Date) as MOST_RECENT_DATE
--,t.Column_Name 
FROM
(
    SELECT [ID]
    ,[Date1] as Date
    ,[T1] as T
--  ,COL_NAME(OBJECT_ID('Table1'), 2) AS Column_Name 
    FROM Table1
UNION ALL
    SELECT [ID]
    ,[Date2] as Date 
    ,[T2] as T 
--    ,COL_NAME(OBJECT_ID('Table1'), 4) AS Column_Name
    FROM Table1
  UNION ALL
    SELECT [ID] 
    ,[Date3] as Date
    ,[T3] as T 
--    ,COL_NAME(OBJECT_ID('Table1'), 6) AS Column_Name
    FROM Table1
  UNION ALL 
    SELECT [ID]
    ,[Date4] as Date
    ,[T4] as T 
--    ,COL_NAME(OBJECT_ID('Table1'), 8) AS Column_Name 
    FROM Table1
) t
WHERE t.T = 'Y'
GROUP BY t.[ID]
--, t.Column_Name
ORDER by [ID]
4

3 回答 3

0

替代GROUP BY+ MAX(),您可以使用CROSS APPLY (SELECT TOP 1). 这将让您返回其他数据以及最大日期:

SELECT
  t.*,
  x.Date,
  x.FlagName
FROM TABLE1 AS t
CROSS APPLY (
  SELECT TOP 1
    Date,
    FlagName
  FROM (
    SELECT Date1, T1, 'T1' UNION ALL
    SELECT Date2, T2, 'T2' UNION ALL
    SELECT Date3, T3, 'T3' UNION ALL
    SELECT Date4, T4, 'T4' UNION ALL
    SELECT Date5, T5, 'T5'
  ) AS s (Date, T, FlagName)
  WHERE T = 'Y'
  ORDER BY Date
) AS x
;
于 2013-04-08T06:50:19.200 回答
0

那个怎么样:

SELECT t.ID, MAX(t.Date) FROM
  (SELECT ID, Date1 as Date, T1 as T FROM Test
  UNION ALL
    SELECT ID, Date2 as Date, T2 as T FROM Test
  UNION ALL
    SELECT ID, Date3 as Date, T3 as T FROM Test
  UNION ALL 
    SELECT ID, Date4 as Date, T4 as T FROM Test) t
WHERE t.T = 'Y'
GROUP BY t.ID

简单演示

于 2013-04-04T05:41:31.787 回答
0

我终于得到了以下查询(偶然)并且它起作用了.....

  1. 我创建了一个如下所示的视图

    CREATE VIEW View_Test 选择 [ID],当 [T1] = 'Y' 然后 [D1] else null end 为 [D1] 时的情况,如果 [T2] = 'Y' 然后 [D2] else null end 作为 [D2] , 当 [T3] = 'Y' then [D3] else null end as [D3], case when [T4] = 'Y' then [D4] else null end as [D4] from Test

然后我在 unpivot 查询中使用了该视图,如下所示

select [ID], max(DATE_FIELD) as most_recent_date, max(Flag) as most_recent_flag
from View_Test
unpivot (
DATE_FIELD for Flag in 
(
[D1]
, [D2]
, [D3]
,[D4]
)
) as DATE_Unpivoted
group by [ID]
ORDER BY [ID]

当我执行上述查询时,“max(flag)”给了我最近日期的日期字段的名称。我仍然不知道它是如何工作的。关于“旗帜”的那一点我仍然无法理解。但我不小心把它留在了查询中......从我尝试过但没有工作的不同查询中复制并粘贴:)

有人可以请教吗?:)

于 2013-04-18T00:46:53.647 回答