Solved

Count rows

Posted on 2009-07-07
4
244 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
[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
  • 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

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.

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 Trigger 8 37
Related to SQL Query 5 21
8 hour Continual coverage based on Time in and Time Out 9 21
T-SQL: problem comparing datetime 4 49
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 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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

733 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