Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Return 0 count when a Department within a CTE contains records for one set, but not another

$
0
0

This is a bit of a complicated question to explain, but I am hoping not as complicated to resolve. I will try to describe this generically to allow anybody who can provide a solution to provide a generic example that anybody could adapt to their data.

In my query, I created a CTE which combines two separate sets of data. One set of data lists who is within a given audience (call it audiencelist). The other set lists people who are in that same audience and have completed a given course (call if courselist). This is then all brought into one table by the CTE.

My resulting report in Visual Studio 2008 is then going to include an expression to count each employee from both lists by department within the same field. (I will achieve this by first grouping by department, and then by the appropriate measure, which in the case of this example, I am calling audiencelist and courselist. Then, I will include an expression that counts the Employees.) The problem is, if a department has somebody in the audience, but NOBODY from that department has completed the course yet, I need the department to still show up in the "courselist" but with a count of 0.

Example of the desired result:

Dept  Field            Count

A       Audiencelist  25
A       Courselist     15
B       Audiencelist  20
B       Courselist     10
C       Audiencelist  20
C       Courselist     0


I was trying as best I can to make this a generic example, but let me know if it would be easier if I share some of my actual code so you can provide a solution. My hope was that somebody could offer a generic example I could adapt to my data.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>