Solved

Special grouped select in a SQL table

Posted on 2013-01-28
3
207 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:
ScottPletcher 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 Copy Database Wizard 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.
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.
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.

747 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