Solved

Count rows

Posted on 2009-07-07
4
232 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 142

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

776 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