Solved

Count rows

Posted on 2009-07-07
4
239 Views
Last Modified: 2012-05-07
I have a table with three columns: Id, TypeId, SubTypeId
Each TypeId has 3 SubTypeId.
The SubTypeId are from 1 to 3.
So That TypeId=1 has SubTypeId  1, 2 or 3
and TypeId=2 has also the SubTypeId 1,2 or 3.
I want to find how many SubTypeId's exist for each TypeId.
How can I do that?

Example:
typeId      subtypeid         Count
1                  1                    5
1                  2                    3
1                  3                    2
2                  1                    7
2                  2                    2
2                  3                    8
...
0
Comment
Question by:johnkainn
  • 2
4 Comments
 
LVL 17

Assisted Solution

by:pssandhu
pssandhu earned 100 total points
ID: 24799025
Select TypeID, COUNT(*) as NumSubTypeID
From Table
Group by TypeID
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 total points
ID: 24799028
SELECT TypeID, COUNT(*) AS SubTypes
FROM YourTable
GROUP BY TypeID
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 24799033
I think you just need to group by:
select typeid, subtypeid, count(*)
  from yourtable
 group by typeid, subtypeid
 order by typeid, subtypeid

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24799037
Or, if you want to count the dsitinct subtypes in the case of repeats...

SELECT TypeID, COUNT( DISTINCT SubTypeID) AS SubTypes
FROM YourTable
GROUP BY TypeID
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

860 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