下面是示例VIEW_TEST表,我要从中选择与1个以上曲目相关联的deviceId。例如。第100个设备与FirstTrack和SecondTrack相关联,因此应选择
我尝试了很多事情,但无法获得答案。以下是我尝试过的查询,但未返回任何内容
select deviceid
from ( SELECT distinct deviceid, trackid , count (distinct deviceid) over (partition by trackid) as cnt
FROM VIEW_TEST) t
where cnt > 0;
当我在内部查询下执行时,
SELECT distinct deviceid, trackid , count (distinct deviceid) over (partition by trackid) as cnt
FROM VIEW_TEST
它给了我以下输出
答案 0 :(得分:0)
您不需要使用窗口功能,而是可以使用:
select t.*
from VIEW_TEST t
where exists (select 1 from VIEW_TEST t1 where t1.deviceid = t.deviceid and t1.trackid <> t.trackid);
您还可以使用group by
子句:
select deviceid
from VIEW_TEST t
group by deviceid
having min(trackid) <> max(trackid);
但是,COUNT(DISTINCT trackid)
也会有所帮助:
select deviceid
from VIEW_TEST t
group by deviceid
having count(distinct trackid) > 1;