SQL Server:左外连接是否存在ID

时间:2014-08-29 15:18:00

标签: sql-server tsql

表格ProviderValueCard中的前三项会在= 1时添加各自的金额。如果表格ProviderID中存在SubscriptionsTV,我还会尝试将50添加到我的总分数中。 GroupID也来自SubscriptionsTV,其中需要满足条件。我相信我需要在两个表的ProviderID列的第二个查询中有一个左外连接。

DECLARE @ProviderID int = '1717';

WITH cte as(
SELECT TOP 1 ProviderID, Time_Stamp,
       SUM(CASE WHEN [AdditionalReports] = '1' THEN 5 ELSE 0 END) as AdditionalReports,
       SUM(CASE WHEN [UniqueReportRequests] = '1' THEN 15 ELSE 0 END) as UniqueReportsRequests,
       SUM(CASE WHEN [SurveyCompleted] = '1' THEN 30 ELSE 0 END) as SurveyCompleted
       --IF @ProviderID EXISTS SUM(THEN 50 ELSE 0 END) 

       FROM ProviderValueCard
       WHERE ProviderID = @ProviderID
       GROUP BY Time_Stamp, ProviderID
       ORDER BY Time_Stamp DESC
    )
        SELECT ProviderID, Time_Stamp, (AdditionalReports + UniqueReportsRequests + SurveyCompleted) AS TotalScore
        FROM cte
        --WHERE GroupID = 2

返回

ProviderID  Time_Stamp              TotalScore
----------- ----------------------- -----------
1717        2014-08-28 13:03:30.593 45

ProviderValueCard表

ProviderID  AdditionalReports UniqueReportRequests SurveyCompleted Time_Stamp
----------- ----------------- -------------------- --------------- -----------------------
1717        0                 1                    1               2014-08-28 13:03:30.593

订阅电视表

ProviderID  GroupID
----------- -----------
1717        2

我的最终结果是:

DECLARE @ProviderID int = '1717';

WITH cte as(
SELECT TOP 1 a.ProviderID, Time_Stamp,
       SUM(CASE WHEN [AdditionalReports] = '1' THEN 5 ELSE 0 END) as AdditionalReports,
       SUM(CASE WHEN [UniqueReportRequests] = '1' THEN 15 ELSE 0 END) as UniqueReportsRequests,
       SUM(CASE WHEN [SurveyCompleted] = '1' THEN 30 ELSE 0 END) as SurveyCompleted,
       MAX(CASE WHEN b.ProviderID IS NULL THEN 0 ELSE 50 END) as SubscriptionExists

       FROM ProviderValueCard a
       LEFT JOIN SubscriptionsTV b
         ON a.ProviderID = b.ProviderID  
       WHERE a.ProviderID = @ProviderID AND GroupID = 2
       GROUP BY Time_Stamp, a.ProviderID, event
       ORDER BY event DESC, Time_Stamp DESC
    )
        SELECT ProviderID, Time_Stamp, (AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore
        FROM cte

1 个答案:

答案 0 :(得分:2)

您可以使用LEFT JOIN来完成此操作,并且MAX()使用CASE语句:

DECLARE @ProviderID int = '1717';    
WITH  Subs AS (SELECT DISTINCT ProviderID
               FROM SubscriptionsTV
               )
     ,cte  AS (SELECT TOP 1 a.ProviderID, Time_Stamp,
                      SUM(CASE WHEN [AdditionalReports] = '1' THEN 5 ELSE 0 END) as AdditionalReports,
                      SUM(CASE WHEN [UniqueReportRequests] = '1' THEN 15 ELSE 0 END) as UniqueReportsRequests,
                      SUM(CASE WHEN [SurveyCompleted] = '1' THEN 30 ELSE 0 END) as SurveyCompleted,
                      MAX(CASE WHEN b.ProviderID IS NULL THEN 0 ELSE 50 END) as SubscriptionExists    
               FROM ProviderValueCard a
               LEFT JOIN Subs b
                 ON a.ProviderID = b.ProviderID  
               WHERE a.ProviderID = @ProviderID
               GROUP BY Time_Stamp, ProviderID
               ORDER BY Time_Stamp DESC
              )
 SELECT ProviderID, Time_Stamp, (AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore
 FROM cte

更新:由于可以存在多个providerID,需要DISTINCT,使用上面的第二个cte,也可以在CASE语句中使用相关的子选择。

没有注意,ORDER BY在使用TOP时在cte中没问题。