从多个SQL列获取最新日期

时间:2015-09-02 15:31:06

标签: sql-server

建议的答案in this post适用于两列。 我有大约50个不同的日期列,我需要能够报告最近的交互,无论表格如何。

在这种情况下,我将列放入视图中,因为它们来自两个不同数据库中的不同表...

    CREATE VIEW vMyView
    AS
    SELECT 
    comp_name AS Customer
    , Comp_UpdatedDate AS Last_Change
    , CmLi_UpdatedDate AS Last_Communication
    , Case_UpdatedDate AS Last_Case
    , AdLi_UpdatedDate AS Address_Change
    FROM Company
    LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
    LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId 
    LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
...

我的问题是,我如何轻易地解释一栏比其他栏更多的可能性?

根据上面的例子,只使用两个第一列,效果很好。但考虑到一行可以将第3列作为最高值,另一行可以有第14列等......

SELECT Customer, MAX(CASE WHEN (Last_Change IS NULL OR Last_Communication> Last_Change)
                THEN Last_Communication ELSE Last_Change 
                END) AS MaxDate
FROM vMyView
GROUP BY Customer

那么,如何轻松地获取50(ish)列中任何一行中每行的最高值?

我正在使用SQL Server 2008 R2,但我还需要它在2012和2014版本中使用。

非常感谢任何帮助。

修改

我刚刚发现第二个数据库将日期存储在NUMERIC字段中,而不是DATETIME中。 (愚蠢!我知道!)

所以我得到错误: 列的类型" ARCUS"与UNPIVOT列表中指定的其他列的类型冲突。

我尝试使用CAST解决此问题,使其达到DATETIME,但这只会导致更多错误。

    ;WITH X AS 
(
    SELECT Customer
          ,Value      [Date]
          ,ColumnName [Entity]
          ,BusinessEmail 
          ,ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Value DESC) rn
    FROM (
    SELECT comp_name AS Customer
        , Pers_EmailAddress AS BusinessEmail
        , Comp_UpdatedDate AS Company
        , CmLi_UpdatedDate AS Communication
        , Case_UpdatedDate AS [Case]
        , AdLi_UpdatedDate AS [Address]
        , PLink_UpdatedDate AS Phone
        , ELink_UpdatedDate AS Email
        , Pers_UpdatedDate AS Person
        , oppo_updateddate as Opportunity
        , samdat.dbo.ARCUS.AUDTDATE AS ARCUS 


        FROM vCompanyPE

        LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
        LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId 
        LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
        LEFT JOIN PhoneLink on Comp_CompanyId = PLink_RecordID 
        LEFT JOIN EmailLink on Comp_CompanyId = ELink_RecordID
        LEFT JOIN vPersonPE on Comp_CompanyId = Pers_CompanyId
        LEFT JOIN Opportunity on Comp_CompanyId = Oppo_PrimaryCompanyId
        LEFT JOIN Orders on Oppo_OpportunityId = Orde_opportunityid

        LEFT JOIN SAMDAT.DBO.ARCUS on IDCUST = Comp_IdCust

        COLLATE Latin1_General_CI_AS 
        WHERE Comp_IdCust IS NOT NULL
        AND Comp_deleted IS NULL
         ) t

     UNPIVOT (Value FOR ColumnName IN 
                (
        Company
        ,Communication
        ,[Case]
        ,[Address]
        ,Phone
        ,Email
        ,Person     
        ,Opportunity
        ,ARCUS

                )
             )up
)


 SELECT Customer
      , BusinessEmail
      ,[Date]
      ,[Entity]
FROM X 
WHERE rn = 1 AND [DATE] >= DATEADD(year,-2,GETDATE()) and BusinessEmail is not null

2 个答案:

答案 0 :(得分:1)

您可以使用CROSS APPLY手动转动字段,然后使用MAX()

SELECT
    vMyView.*,
    greatest.val
FROM
    vMyView
CROSS APPLY
(
    SELECT
        MAX(val) AS val
    FROM
    (
              SELECT vMyView.field01 AS val
    UNION ALL SELECT vMyView.field02 AS val
    ...
    UNION ALL SELECT vMyView.field50 AS val
    )
        AS manual_pivot
)
    AS greatest

最内层的查询会将每个字段转移到一个新行,然后MAX()将它们重新聚合回一行。 (同时跳过NULL,因此您不需要明确地为它们提供服务。)

答案 1 :(得分:0)

;WITH X AS 
(
    SELECT Customer
          ,Value      [Date]
          ,ColumnName [CommunicationType]
          ,ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Value DESC) rn
    FROM (
    SELECT comp_name AS Customer
        , Comp_UpdatedDate AS Last_Change
        , CmLi_UpdatedDate AS Last_Communication
        , Case_UpdatedDate AS Last_Case
        , AdLi_UpdatedDate AS Address_Change
        FROM Company
        LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
        LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId 
        LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
    ) t
     UNPIVOT (Value FOR ColumnName IN (Last_Change,Last_Communication,
                                   Last_Case,Address_Change))up
 )
 SELECT Customer
      ,[Date]
      ,[CommunicationType]
FROM X 
WHERE rn = 1