TSQL: search for unique records in one table

Consider a table or CTE, structured as follows:

Name Num ---- ---- Abc 12 Abc 12 XYZ 70 XYZ 80 XYZ 85 Bar 50 Bar 55 Foo 44 Foo 44 Baz 88 

The requirement is to define a Name where several different Nums exist.

Desired result set

 Name ---- XYZ Bar 

Which TSQL statement would you use to get this result set?

Update: indeed, there can be 2+ entries for this name.

+4
source share
1 answer

Assumes NULL in a Num column

 select Name from MySetObject group by name having min(num) <> max(num) -- also COUNT(DISTINCT Num) > 1 achieves the same 
+9
source

Source: https://habr.com/ru/post/1307380/


All Articles