• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

SQL Count

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,

Thanks for your quick reply, thats a great help.

Cheers,
Stephen.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem. But was it what you wanted?
 
Just the other day, I was thinking ... about this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic