?
Solved

SQL Query to Count Entries Per Column Value

Posted on 2009-04-14
3
Medium Priority
?
639 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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