Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

SQL Count

 
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.
 
whippersnapper
Posts: 1843
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
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. But was it what you wanted?
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic