0

I currently have two separate queries in SQL Server that count the number of times a table contains a unique ID in a week. I would like to display these using one query, not two.

This data is held in two separate views, hence my writing two queries. These are ActivityPointer and Asp_dealercallreport.

Query #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    

Query #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

Thanks

1
  • Usually, you can use some of the system tables, which contains only one row or create a temporary one, and then solve both queries as a subquery, so you will have one row, and two fields on the same query. Commented Jan 18, 2015 at 20:05

3 Answers 3

1

Maybe you can simply use INNER JOIN operator? Like this:

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    
Sign up to request clarification or add additional context in comments.

1 Comment

Wizardry. Thank you. I need to brush up on my JOINs. When I was doing this I was coming up with some strange results.
1

Use conditional aggregation:

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 Comment

Aren't you missing the point that there are actually two tables used in the question?
1

Using a standard SQL UNION will provide you with the output of both queries in one result set. As long as they have same number and type of columns:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.