Solved

Count rows

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now