另一个表中的SQL Server MAX Date

时间:2018-01-31 14:03:49

标签: sql sql-server tsql

我有点陷入困境,并希望有人可以用我的方式指出错误。我有一个更新,我写了一个case语句,如果case语句条件我有一个ELSE来从另一个表获取最大日期。我最初将其作为ELSE MAX(CONVERT(VARCHAR(23),vp.CancelDate,121)) END并遇到错误:

  

聚合可能不会出现在UPDATE语句的集合列表中。

我做了一些研究,并把它放在一个选择声明中,但我仍然得到同样的错误。我显然没有做到这一点。有人可以告诉我如何解决这个问题吗?

   SELECT CASE WHEN SFP.Product_Cancellation_Date__c IS NOT NULL AND cp.MainCode NOT IN ('AYE3','AYE4','AYE7') AND SFP.ABC_PRODUCT__c='AYE1' THEN CONVERT(VARCHAR(23),SFP.Product_Cancellation_Date__c ,121) 
            WHEN SFP.Product_Cancellation_Date__c IS NOT NULL AND cp.MainCode IN ('AYE3','AYE4','AYE7' ) AND SFP.ABC_PRODUCT__c='AYE2' THEN CONVERT(VARCHAR(23),SFP.Product_Cancellation_Date__c,121) 
     ELSE (select MAX(CONVERT(VARCHAR(23),vp.CancelDate,121)) from tblProducts) END --HERE IS WHERE MY ISSUE IS
     FROM [DB_User].[dbo].[Customer] s
    INNER JOIN [EMP].dbo.tblProducts vp ON vp.customerID=s.customerID
    INNER JOIN tblXXProducts cp ON vp.ProductID=cp.ProductID
    INNER JOIN [LINKEDSERVER].CBS.SF.tblRNRProduct__c SFP
    ON SFP.Account_Number__c=CAST(s.CustomerID AS VARCHAR)
    AND SFP.ABC_PRODUCT__c=CASE WHEN cp.MainCode IN ('AYE3','AYE4','AYE7' ) THEN 'XYZ' ELSE 'ERD' END
   WHERE s.Status='C' AND s.CustomerID='36598758';

3 个答案:

答案 0 :(得分:0)

阅读您的代码

select MAX(CONVERT(VARCHAR(23),vp.CancelDate,121)) from tblProducts

此处您不应使用vp.CancelDate,因为您试图从tblProducts单独获取最大值

我建议您使用以下内容:

 SELECT CASE WHEN SFP.Product_Cancellation_Date__c IS NOT NULL AND cp.MainCode NOT IN ('AYE3','AYE4','AYE7') AND SFP.ABC_PRODUCT__c='AYE1' THEN CONVERT(VARCHAR(23),SFP.Product_Cancellation_Date__c ,121) 
            WHEN SFP.Product_Cancellation_Date__c IS NOT NULL AND cp.MainCode IN ('AYE3','AYE4','AYE7' ) AND SFP.ABC_PRODUCT__c='AYE2' THEN CONVERT(VARCHAR(23),SFP.Product_Cancellation_Date__c,121) 
     ELSE x.CancelDate END 
     FROM [DB_User].[dbo].[Customer] s
    INNER JOIN [EMP].dbo.tblProducts vp ON vp.customerID=s.customerID
    INNER JOIN (
       (select MAX(CONVERT(VARCHAR(23),CancelDate,121)) as CancelDate from tblProducts)
    ) as x on vp.customerID = s.customerID
    INNER JOIN tblXXProducts cp ON vp.ProductID=cp.ProductID
    INNER JOIN [LINKEDSERVER].CBS.SF.tblRNRProduct__c SFP
    ON SFP.Account_Number__c=CAST(s.CustomerID AS VARCHAR)
    AND SFP.ABC_PRODUCT__c=CASE WHEN cp.MainCode IN ('AYE3','AYE4','AYE7' ) THEN 'XYZ' ELSE 'ERD' END
   WHERE s.Status='C' AND s.CustomerID='36598758';

希望这会对你有所帮助

答案 1 :(得分:0)

从您的评论中我认为以下内容可能有效,即使它可能不是实现您想要的最有效方式:

SELECT 
    CASE
        WHEN [SFP].[Product_Cancellation_Date__c] IS NOT NULL
             AND [cp].[MainCode] NOT IN('AYE3', 'AYE4', 'AYE7')
             AND [SFP].[ABC_PRODUCT__c] = 'AYE1'
        THEN CONVERT(VARCHAR(23), [SFP].[Product_Cancellation_Date__c], 121)
        WHEN [SFP].[Product_Cancellation_Date__c] IS NOT NULL
             AND [cp].[MainCode] IN('AYE3', 'AYE4', 'AYE7')
             AND [SFP].[ABC_PRODUCT__c] = 'AYE2'
        THEN CONVERT(VARCHAR(23), [SFP].[Product_Cancellation_Date__c], 121)
        ELSE
             (SELECT 
                  MAX(CONVERT(VARCHAR(23), [p].[CancelDate], 121))
              FROM [tblProducts] [p]
              WHERE [p].[customerID] = [s].[customerID])
    END --HERE IS WHERE MY ISSUE IS
FROM [DB_User].[dbo].[Customer] [s]
INNER JOIN [EMP].[dbo].[tblProducts] [vp]
     ON [vp].[customerID] = [s].[customerID]
INNER JOIN [tblXXProducts] [cp]
     ON [vp].[ProductID] = [cp].[ProductID]
INNER JOIN [LINKEDSERVER].[CBS].[SF].[tblRNRProduct__c] [SFP]
     ON [SFP].[Account_Number__c] = CAST([s].[CustomerID] AS VARCHAR)
        AND [SFP].[ABC_PRODUCT__c] = CASE
                                         WHEN [cp].[MainCode] IN('AYE3', 'AYE4', 'AYE7')
                                         THEN 'XYZ'
                                         ELSE 'ERD'
                                     END
WHERE [s].[Status] = 'C'
      AND [s].[CustomerID] = '36598758';

请注意,在SELECT声明中,您为整个查询中选择的每个客户检索MAX列的CancelDate值,在您的情况下,只会由于最终WHERE声明而成为ID值为36598758的客户。

这当然是未经测试的,因为我没有CREATE TABLE语句和一些初始数据。

答案 2 :(得分:0)

你不能像这样交叉表 我很惊讶你甚至得到多行错误

select MAX(CONVERT(VARCHAR(23), vp.CancelDate, 121)) from tblProducts

下面不是解决方法 - 只是试图解决问题

SELECT CASE WHEN SFP.Product_Cancellation_Date__c IS NOT NULL 
                 AND cp.MainCode NOT IN ('AYE3', 'AYE4', 'AYE7') 
                 AND SFP.ABC_PRODUCT__c='AYE1' 
            THEN CONVERT(VARCHAR(23), SFP.Product_Cancellation_Date__c , 121) 
            WHEN SFP.Product_Cancellation_Date__c IS NOT NULL 
                 AND cp.MainCode     IN ('AYE3', 'AYE4', 'AYE7') 
                 AND SFP.ABC_PRODUCT__c='AYE2'  
            THEN CONVERT(VARCHAR(23), SFP.Product_Cancellation_Date__c, 121) 
            ELSE (select MAX(CONVERT(VARCHAR(23), vp.CancelDate, 121)) 
                  from tblProducts)  --HERE IS WHERE MY ISSUE IS
       END as 'xxx'
 FROM [DB_User].[dbo].[Customer] s
 JOIN [EMP].dbo.tblProducts vp 
   ON vp.customerID = s.customerID
  AND                 s.Status='C' 
  AND                 s.CustomerID = '36598758';
 JOIN tblXXProducts cp 
   ON vp.ProductID = cp.ProductID
 JOIN [LINKEDSERVER].CBS.SF.tblRNRProduct__c SFP
   ON SFP.Account_Number__c = CAST(s.CustomerID AS VARCHAR)
  AND SFP.ABC_PRODUCT__c = CASE WHEN cp.MainCode IN ('AYE3','AYE4','AYE7') 
                                THEN 'XYZ' 
                                ELSE 'ERD' 
                           END