Solved

SQL Query to Count Entries Per Column Value

Posted on 2009-04-14
3
636 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 250 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 250 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

I recently went through setting up a JasperReports Server using the AWS EC2 instance, and this article will cover some basic administration tasks I had to perform.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

687 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