两个表之间的SQL计数

时间:2015-01-18 20:01:10

标签: sql sql-server join count

我目前在SQL Server中有两个单独的查询,用于计算表在一周内包含唯一ID的次数。我想使用一个查询显示这些,而不是两个。

此数据保存在两个单独的视图中,因此我编写了两个查询。这些是ActivityPointerAsp_dealercallreport

查询#1:

SELECT 
    OwnerIDName, 
    COUNT(Distinct ActivityID) AS CalendarEvents
FROM 
    ActivityPointer
WHERE
    /*Specify Activity code for Calendar Events*/
    ActivityTypeCode = '4201'
    /*Specify Calendar Events from this week only*/
    AND ScheduledStart >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0)
    AND ScheduledStart <= DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
    /*Specify users to be reported on by Name*/
    AND OwnerIdName IN ('John Doe', 'Jane Doe')
GROUP BY 
    OwnerIDName    

查询#2:

SELECT 
    OwnerIDName, 
    COUNT(Distinct Asp_dealercallreportId) AS DealerVisits
FROM 
    Asp_dealercallreport
    /*Specify Calendar Events from this week only*/
WHERE    
    asp_callreportdate >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0)
    AND asp_callreportdate <= DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
    /*Specify  to be reported on by Name*/
    AND OwnerIdName IN ('John Doe', 'Jane Doe')
GROUP BY 
    OwnerIDName

由于

3 个答案:

答案 0 :(得分:1)

使用条件聚合:

SELECT OwnerIDName,
       COUNT(Distinct case when ScheduledStart >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0) AND
                                ScheduledStart <= DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
                           then ActivityID end) AS CalendarEvents_Scheduled,
       COUNT(Distinct case when asp_callreportdate >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0) and
                                asp_callreportdate <= DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
                           then ActivityID end) AS CalendarEvents_asp
FROM ActivityPointer
WHERE ActivityTypeCode = '4201' AND
      OwnerIdName IN ('John Doe', 'Jane Doe')
GROUP BY OwnerIDName;

答案 1 :(得分:1)

也许你可以简单地使用INNER JOIN运算符?像这样:

SELECT 
    ap.OwnerIDName, 
    COUNT(Distinct ap.ActivityID) AS CalendarEvents, 
    COUNT(Distinct a_dcr.Asp_dealercallreportId) AS DealerVisits
FROM 
    ActivityPointer ap
INNER JOIN 
    Asp_dealercallreport a_dcr ON ap.OwnerIDName=a_dcr.OwnerIDName
WHERE
/*Specify Activity code for Calendar Events*/
    ap.ActivityTypeCode = '4201'
/*Specify Calendar Events from this week only*/
    AND ap.ScheduledStart >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0)
    AND ap.ScheduledStart <= DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
/*Specify Calendar Events from this week only*/
    AND a_dcr.asp_callreportdate >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0)
    AND a_dcr.asp_callreportdate <= DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
/*Specify users to be reported on by Name*/
    AND ap.OwnerIdName IN ('John Doe', 'Jane Doe')
GROUP BY 
    ap.OwnerIDName    

答案 2 :(得分:1)

使用标准SQL UNION将在一个结果集中为您提供两个查询的输出。只要它们具有相同数量和类型的列:

http://www.w3schools.com/sql/sql_union.asp

SELECT OwnerIDName, COUNT(Distinct ActivityID) AS CalendarEvents
FROM ActivityPointer
WHERE
/*Specify Activity code for Calendar Events*/
    ActivityTypeCode = '4201'
/*Specify Calendar Events from this week only*/
AND ScheduledStart >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0)
AND ScheduledStart <= DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
/*Specify users to be reported on by Name*/
AND OwnerIdName IN ('John Doe', 'Jane Doe')
GROUP BY OwnerIDName    
UNION
SELECT OwnerIDName, COUNT(Distinct Asp_dealercallreportId) AS DealerVisits
FROM Asp_dealercallreport
/*Specify Calendar Events from this week only*/
WHERE    asp_callreportdate >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7  * 7, 0)
AND    asp_callreportdate <= DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
/*Specify  to be reported on by Name*/
AND    OwnerIdName IN ('John Doe', 'Jane Doe')
GROUP BY OwnerIDName