Solved

Count rows

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server - Slabs 9 40
sql Total query 2 18
Rename SQL Instance/SQL Developer Edition 2012 2 12
SQL - SP needs a little help 9 26
In this article I will describe the Detach & Attach 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 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

22 Experts available now in Live!

Get 1:1 Help Now