Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL Count

 
Stephen ODonnell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a table which contains a list of different error codes. I want to do a count of each different error code i.e code 3000 = 3, code 4000 = 10, etc but can't figure it out (I'm new to sql)
I've tried a few different sql statements, such as:

which will print out the number of different codes.

Any help greatly appreciated,
Thanks,
Stephen.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1826
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Read up on how count() and group by work. (Some versions of SQL may allow you to omit the group by section in this particular query, but most versions require it.)

My proposed query is based on some assumptions about what you trying to do. Correct me if I'm wrong.

I have a table which contains a list of different error codes

A setup table listing all possible valid error codes or error codes representing errors that were actually recorded? I assumed the latter.

3000 = 3, code 4000 = 10

So error code 3000 has been recorded 3 times; error code 4000, 10 times; right? Right off the bat this suggests that you want to have two columns in your results, along the lines of

error_code how many times
---------- --------------
30003
400010
523417
etc.

but you have only 1 item, COUNT(DISTINCT Message_code), in your select list, so you know your original query can't be right.

In SQL speak, you essentially want to make a group for each value of message_code (that's what the group by message_code does) and count the number of rows that occurs for each value of the grouped by field (that's what the count(*) does).
 
Stephen ODonnell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

Thanks for your quick reply, thats a great help.

Cheers,
Stephen.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1826
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. But was it what you wanted?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic