[UPHPU] OT: SQL question (MSSQL)

J M jmul.php at gmail.com
Wed Sep 6 19:59:18 MDT 2006

I have a SQL question.  Please let me know if it is too far off topic and
I'll take this question elsewhere.  I have to work with MSSQL but I think I
might be able to translate, if I have to, your suggestions based on your
knowledge of other rdbms's to make it work with MSSQL.  I'm looking around
for an MSSQL mailing group to join that doesn't dive too far into .Net

I asked this question on the IRC channel but I'm still having a problem.  I'm
trying to run a query that will return a charge_code and a count of how many
times this charge code was used.  I'm also getting other data back but my
question has to do with the charge code and the count.

I've rearranged the code and tried all the ideas I could think of and I'm
still not getting the desired result.  I want all 6300+ charge codes listed
in my result whether or not they were used.  If they weren't used I want a
zero in the count column.  What is happening is every single record contains
the total number of all the charge codes used in the time frame specified.

Example of what I'm getting…

chg_code         count

0060N             24052

0061N             24052

00961              24052

0552N             24052


Example of what I'd like to get…

chg_code         count

0060N             15

0061N             259

00961              0

0052N             72


Would I need a subquery with some sort of loop that adds up the count?  If
so, I've never done loops in SQL.  Are there any good web references?  (Books
Online isn't helping me any)  If using a loop is too complicated for this
problem, is there another (better) way to approach this?


More information about the UPHPU mailing list