Solved

Special grouped select in a SQL table

Posted on 2013-01-28
3
218 Views
Last Modified: 2013-01-28
I have a table T with column A,B,C,...
The Primary Key is A,B,C
I need a select to list all A and the number of distinct B they have
So if the table is as follows :
A1  B1  C1
A1  B1  C2
A1  B2  C3
A1  B2  C4
A1  B2  C5
A2  B1  C1
A3  B1  C2

The result of the select is
A1,2
A1,1
A3,1
0
Comment
Question by:LeTay
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 38827456
SELECT colA, count(distinct colb)
FROM dbo.tablename
GROUP BY colA
0
 
LVL 6

Expert Comment

by:esolve
ID: 38827466
SELECT tbl.A, COUNT(tbl.B) FROM
(
SELECT DISTINCT t.A,t.B FROM T
INNER JOIN T as T2 ON t.A = t2.A AND t.B = t2.B AND t.C = t2.C
)  tbl GROUP BY tbl.A

Haha ok Scott you win. I did not see the count so had to adjust mine. His answer is correct and easier
0
 

Author Comment

by:LeTay
ID: 38828905
Indeed, first select is beautiful !
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

763 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