[quoted text, click to view] > does i.c1 <= d.c1 happen for every value in the set?
Yes. This is a self-cross join so the c1 <= c1 comparison happens for every
possible pair of C1 values in the Dist table. As you've already said,
COUNT(<expression>) ignores any NULL values of the expression. That
expression can be anything, it doesn't have to be just a column name. In
this instance it's a CASE construct. To see how it works, put the COUNT
statement in the SELECT list and remove the HAVING clause:
SELECT d.c1,COUNT(CASE WHEN i.c1 <= d.c1 THEN 1 ELSE NULL END)
FROM #dist d CROSS JOIN #dist i
GROUP BY d.c1
Result:
c1
----------- -----------
1 1
2 2
3 3
5 4
8 5
You can see that the extra column effectively computes a sequential number
because there is one c1 value less than or equal to 1, two c1 values less
than or equal to 2,... etc. The COUNT(<expression>) function is counting the
NON-NULL results of the CASE expression for each d.c1 value. Lookup CASE in
Books Online if you aren't familiar with CASE expressions. To disect this
query even further you can try this query, which shows what the result of
the JOIN is before GROUP BY:
SELECT i.c1, d.c1, CASE WHEN i.c1 <= d.c1 THEN 1 ELSE NULL END
FROM #dist d CROSS JOIN #dist i
It's difficult to describe but once you've got it you'll get it for all
time!
If you try inserting a duplicate value into Dist you'll notice that the
Median calculation breaks down because of the way the COUNT(CASE...)
expression works with the duplicate values. If you Google in this group for
Median you'll find other suggested solutions for the median function.
Hope this helps.
--
David Portas
SQL Server MVP
--