需要帮助将第二个查询合并到第一个查询中

时间:2016-11-03 20:14:53

标签: sql sql-server

我根据需要运行第一个查询。此查询按周将用户的活动级别聚合到相应的BusinessIds中。

出于报告目的,我需要创建一个新列,用于捕获每个Grouped Week和BusinessId的活动级别大于0的UserId数。第二个查询自己给了我正确的值(尽管它可能有比合并后所需的数据点更多的数据点),但是我很难将它合并到我的第一个查询中。

这可能是一项非常直接的努力,但我无法从其他问题的样本中找到答案。任何帮助表示赞赏。

/* First Query */
SET DATEFIRST 2;
SELECT 
     SUM(
         fua.[RisksAffected]
        +fua.[IssuesAffected]
        +fua.[ChangesAffected]) AS RIC
    ,SUM(
         fua.[ProjectsCreated]
        +fua.[ProjectsAffected]) AS Projects
    ,CASE
       WHEN SUM(fua.[ProjectsCreated]) >0 
          THEN CAST(1 AS BIT)
          ELSE CAST(0 AS BIT) 
          END AS ProjectCreated
   ,SUM(fua.[TimesheetsCreated]) AS Timesheets
   ,SUM(fua.[ReportsAffected]) AS Reports
   ,SUM(fua.[FilesAffected]) AS Files
   ,ud.[BusinessId]
   ,COUNT ( DISTINCT fua.UserId ) AS [UserCount] 
   ,DateAdd(day, -1 * datepart(dw,fua.[QueryDate]),fua.[QueryDate]) AS [ActivityDate]   

FROM [client_projectmanager].[whs].[FactUserActivity] fua
   Left Join [client_projectmanager].[whs].[DimUserDataV2] ud
      ON
   fua.[UserId] = ud.[UserId]

GROUP BY 
   DateAdd(day, -1 * datepart(dw,fua.[QueryDate]),fua.[QueryDate]),ud.[BusinessId]


/* Second Query */
SET DATEFIRST 2; 
SELECT
      count (distinct sela.UserId)
      ,sela.BusinessId
      ,DateAdd(day, -1 * datepart(dw,sela.[Date]),sela.Date)
FROM 
    (SELECT 
       fua.UserId
       ,ud.BusinessId
       ,DateAdd(day, -1 * datepart(dw,fua.[QueryDate]),fua.[QueryDate]) AS [Date]
       ,SUM(
            fua.[RisksAffected]
           +fua.[IssuesAffected]
           +fua.[ChangesAffected]
           +fua.[ProjectsCreated]
           +[ProjectsAffected]) AS Acttotal

FROM [client_projectmanager].[whs].[FactUserActivity] fua 
       Left Join [client_projectmanager].[whs].[DimUserDataV2] ud
       ON
       fua.[UserId] = ud.[UserId]

GROUP BY 
        DateAdd(day, -1 * datepart(dw,fua.[QueryDate]),fua.[QueryDate])
        ,fua.[UserId]
        ,ud.BusinessId

HAVING 
        SUM(fua.[RisksAffected]
           +fua.[IssuesAffected]
           +fua.[ChangesAffected]
           +fua.[ProjectsCreated]
           +fua.[ProjectsAffected])
           >0) sela 

 GROUP BY 
        DateAdd(day, -1 * datepart(dw,sela.[Date]),sela.Date)
        ,sela.BusinessId
        ORDER BY DateAdd(day, -1 * datepart(dw,sela.[Date]),sela.Date)

1 个答案:

答案 0 :(得分:0)

如果您想在一个声明中执行此操作,可以使用CTE。

SET DATEFIRST 2;
WITH cte1 AS (
    /* First Query */
    SELECT  SUM(fua.[RisksAffected] + fua.[IssuesAffected] + fua.[ChangesAffected]) AS RIC,
            SUM(fua.[ProjectsCreated] + fua.[ProjectsAffected]) AS Projects,
            CASE WHEN SUM(fua.[ProjectsCreated]) > 0 THEN CAST(1 AS BIT)
                 ELSE CAST(0 AS BIT)
            END AS ProjectCreated,
            SUM(fua.[TimesheetsCreated]) AS Timesheets,
            SUM(fua.[ReportsAffected]) AS Reports,
            SUM(fua.[FilesAffected]) AS Files,
            ud.[BusinessId],
            COUNT(DISTINCT fua.UserId) AS [UserCount],
            DATEADD(day,-1 * DATEPART(dw,fua.[QueryDate]),fua.[QueryDate]) AS [ActivityDate]
    FROM    [client_projectmanager].[whs].[FactUserActivity] fua
            LEFT JOIN [client_projectmanager].[whs].[DimUserDataV2] ud ON fua.[UserId] = ud.[UserId]
    GROUP BY DATEADD(day,-1 * DATEPART(dw,fua.[QueryDate]),fua.[QueryDate]),
            ud.[BusinessId]
),
cte2 AS (
    /* Second Query */
    SELECT  COUNT(DISTINCT sela.UserId),
            sela.BusinessId,
            DATEADD(day,-1 * DATEPART(dw,sela.[Date]),sela.Date)
    FROM    (SELECT fua.UserId,
                    ud.BusinessId,
                    DATEADD(day,-1 * DATEPART(dw,fua.[QueryDate]),fua.[QueryDate]) AS [Date],
                    SUM(fua.[RisksAffected] + fua.[IssuesAffected] + fua.[ChangesAffected] + fua.[ProjectsCreated] + [ProjectsAffected]) AS Acttotal
             FROM   [client_projectmanager].[whs].[FactUserActivity] fua
                    LEFT JOIN [client_projectmanager].[whs].[DimUserDataV2] ud ON fua.[UserId] = ud.[UserId]
             GROUP BY DATEADD(day,-1 * DATEPART(dw,fua.[QueryDate]),fua.[QueryDate]),
                    fua.[UserId],
                    ud.BusinessId
             HAVING SUM(fua.[RisksAffected] + fua.[IssuesAffected] + fua.[ChangesAffected] + fua.[ProjectsCreated] + fua.[ProjectsAffected]) > 0
            ) sela
    GROUP BY DATEADD(day,-1 * DATEPART(dw,sela.[Date]),sela.Date),
            sela.BusinessId
    ORDER BY DATEADD(day,-1 * DATEPART(dw,sela.[Date]),sela.Date)
)
SELECT  *
FROM    cte1
        -- USE INNER JOIN IF YOU WANT ONLY RECORDS THAT EXIST IN BOTH
        LEFT JOIN cte2 ON cte1.BusinessId = cte2.BusinessId
                     AND cte1.ActivityDate = cte2.GroupedByDateName

您的第二个查询缺少列别名..假设您正在加入BusinessId的2个查询和您分组的日期,则需要添加列别名