GIS_USER GIS_USER -4 years ago 117
SQL Question

Count distinct rows where another column not the same

I would like to know how to do a count on NUM, where CODE is not distinct?

So the count would be Two!?

CODE NUM
T1 1001
T1 1001
T1 1001
T2 1002
T2 1002
T3 1003
T3 1003
T4 1003
T5 1004
T5 1004
T5 1004
T6 1005
T7 1005
T7 1005

Answer Source
 Try Below code :

 SELECT NUM , COUNT(*)
 FROM #table
 GROUP BY NUM
 HAVING COUNT(DISTINCT CODE) > 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download