?
Solved

SQL Query to Count Entries Per Column Value

Posted on 2009-04-14
3
Medium Priority
?
643 Views
Last Modified: 2012-05-06
My SQL table has two fields, id and tableName as can be seen in my CREATE statement below.  I want to query the table, counting the number of id's per distinct tableName.  If there were a total of 120 entries in the table, my report might look something like this...

  # IDs Per Table     tableName
  ----------------------------------------------------------
             15               ZipCode
            100              Race
                5              Gender

I don't want to list every entry; just the distinct entries, and the number of entries for that distinct, unique tableName entry.

Here's my current SQL query...

SELECT count(tableName) as "# IDs Per Table" , tableName  FROM (SELECT tableName FROM STD_CheckID GROUP BY tableName) ORDER BY tableName

What it produces looks more like this...

  # IDs Per Table     tableName
  ----------------------------------------------------------
            120               ZipCode
            120               Race
            120               Gender

Can someone help?
CREATE TABLE STD_CHECKID
(    id NUMERIC(20) not null, tableName VARCHAR(200),
    CONSTRAINT pk_STD_CHECKID PRIMARY KEY (id,tableName) USING INDEX TABLESPACE sdsndx
) TABLESPACE sdstab;

Open in new window

0
Comment
Question by:LarryAndro
3 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 1000 total points
ID: 24140106
you don't need the subselect  group by will make a distinct list on its own

SELECT count(tableName) as "# IDs Per Table" , tableName
FROM STD_CheckID
GROUP BY tableName
ORDER BY tableName
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 1000 total points
ID: 24143144
Yep, agree a group by problem...

SELECT count(*) as "# IDs Per Table" , tableName  
FROM STD_CheckID
GROUP BY tableName
0
 
LVL 1

Author Closing Comment

by:LarryAndro
ID: 31569999
I apologize for not closing this question and for not awarding points until now.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question