Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL- retrieving distinct ID

Tags:

t-sql

I have a table with the following data:

ID   NAME    ATTRIBUTE   CODE
=============================
1     XX         MM        GC
1     XX         ST        GC
2     ZZ         LL        GC
2     ZZ         ST        GC
3     AA         MM        PC

I need the ID, name of from the table which contains either MM Attribute or GC code but not both. Like the query should retrieve the ID numbers only 2 and 3 but not 1 .

How do I do that?

like image 796
krishr Avatar asked Jun 13 '26 00:06

krishr


1 Answers

Select Id, Name

From #t
Where (Attribute = 'MM' Or Code = 'GC')
And Id Not In (Select Id From #t 
                Where (Attribute = 'MM' And Code = 'GC'))
like image 122
codingbadger Avatar answered Jun 16 '26 10:06

codingbadger