SQL Query to get only those rows with only 1 value in another column -


i have table this:

sample table

i need query return projectid's have state=21. i.e want projectid 2 & 5.

i not want records projectid 1, 3, 4 & 6, because in case, state equal other numbers

reasoning goes

  • in subselect, select projectid's have state = 21
  • in outer select, retain projectid's have state = 21 using having clause

sql statement

select  projectid    table t1         inner join (           select  projectid              table             state = 21         ) t2 on t2.projectid = t1.projectid group         projectid having  count(*) = 1         

Comments