|
I thought I'd use you're expertise on this since I'm not doing well with it so far and I can't find this answer here with a search. I want a count of unique departments, but grouped by Client. This works for a single client: But, I need to run this on a table with multple clients, so somehow need to group on clients. The results should be: (edit: sorry - actually the results are much simpler than the original answer) (By the way, I didn't see a tag for either COUNT or DISTINCT)
(comments are locked)
|
With your new result requirements maybe this is it: Blackhawk - thanks and my comment to Thomas applies to you too). This looks right - let me test it some... I couldn't get COUNT and DISTINCT to work together this way for some reason.
Jul 29 '10 at 08:40 AM
Mark
Yes, you have it right Blackhawk and such a simple solution - I'm embarrased now darn it! Oh well, I'll know from now on. COUNT will play well with DISTINCT if you can get the syntax right.
Jul 29 '10 at 08:44 AM
Mark
Yup... SQL. It takes 2 seconds to learn it - a lifetime to master it.
Jul 29 '10 at 08:46 AM
Blackhawk-17
+1 The last statement in the answer is perfect. From the question definition, it looks like this is exactly what Mark needs.
Jul 29 '10 at 08:49 AM
Oleg
(comments are locked)
|
|
You can run aggregates against multiple columns. Following your example: Sorry Thomas, my ansewer was not correct at first. I just want a count (by client) of distinct departments. (+1 anyway - you're right)
Jul 29 '10 at 08:35 AM
Mark
In that case and Blackhawk's beaten me to it.
Jul 29 '10 at 08:40 AM
ThomasRushton ♦
Back and forth we go...
Jul 29 '10 at 08:41 AM
Blackhawk-17
I've tested mine now. Both work. ;-)
Jul 29 '10 at 08:42 AM
ThomasRushton ♦
(comments are locked)
|
|
I think this is what you are looking for give this result Thanks Fatherjack, +1 for you too.
Jul 29 '10 at 08:51 AM
Mark
(comments are locked)
|
|
Though Jonathan Allen script is better than this but we can do this way also right? how about this with abc(clientID,DepartmentID,countdepartmentID) as ( select clientID,DepartmentID,COUNT(departmentID) from #testy group by clientID,DepartmentID) select clientid,COUNT(departmentID) from abc group by ClientID
(comments are locked)
|


@Mark Your original query for a single client is unreasonably expensive because it uses a subselect when it does not have to. You could restate it to:
This also means that the answer offered by Blackhawk-17 is perfect (the last statement in the answer).
Oleg, Yes, I was frustrtated trying to get COUNT and DISTINCT to work together, so I started resorting to that convoluted method.
[meta] Can we add tags for COUNT and DISTINCT? Or are they too specific?
@Mark This happened to me in the past too when I needed to figure out how to use count and distinct together for the first time. The trouble is that distinct (without count) exists exclusively for wimps, there is no distinct (without count) in T-SQL. What I mean is:
select distinct some_column from some_table;is inevitably getting translated by the parser to a more proper
select some_column from some_table group by some_column;Where distinct actually shines is as an optional parameter to the count. The actual definition of count is:
count ({[[ all | distinct ] expression] | * })As you can see, if none of the 2 is specified, all is assumed (spelled out for you by the parser). This is what makes the proper usage unclear. If we always had to spell out one of the 2 words then we would never be unclear how to use it:
select count(all some_column) from some_table;
instead of simply
select count(some_column) from some_table;
The former would immediately hint us how to use distinct and count :)
I'm used to using DISTINCT by itself rather than in a function format with () involved (e.g., SELECT DISTINCT ClientID...).