SQLSEVER 如何输出的值可以满足两列值都不重复(看图说话)

img

img


一开始我以为用distinct就可以了,但是输出结果还多出来一个session1,题目是只输出只有NULL的sessionid,还得是不重复的,学生党一枚,这问题可能很简单,大能勿喷,感谢感谢

select distinct sessionid 
from table_3 a 
where not exists (select 1 from table_3  b where a.sessionid =b.sessionid  and b.path is not  null)

一般用上面这个写法就好了,比较好理解,
当然,下面的这个也可以输出相同的结果,代码更短

select a.sessionid  
from table_3  
group by a.sessionid 
having count(path) =0

有些初学者可能会像下面这样写,不能算错,但某些情况下,查询效率会比较低

select distinct sessionid 
from table_3 a 
where sessionid  not in  (select sessionid   from table_3  b where b.path is not  null)