Search code examples
sqlsql-serverdatabasesql-server-2014sql-server-2014-express

Select all rows having specific value assigned only once


I am confused that how i can query this from table below. I have to get only those categories which is assigned to a single group.

**Id**          **Category**   **PracticeGroup**
1               Category-1     Practice Group-1
2               Category-1     Practice Group-1
3               Category-2     Practice Group-2
4               Category-1     Practice Group-1
5               Category-2     Practice Group-1

As in above scenario Category-1 will be the result set which has only one assigned practice group "Practice Group-1".


Solution

  • Just can do this :-

    select [category] from (select distinct [category] ,[Practice Group] from tbl) as temp
    group by [category]
    having count([category]) = 1