Solved

Special grouped select in a SQL table

Posted on 2013-01-28
3
230 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
[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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

626 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