如何仅从所有间隔都具有值的地方选择?

时间:2019-05-17 12:22:40

标签: sql oracle

我想从表中选择,其中所有15分钟间隔的每个通道都有一个值,否则不要选择该间隔。表格:

enter image description here

结果应为:

enter image description here

由于没有通道2值,因此不应包括间隔201905151300。

2 个答案:

答案 0 :(得分:0)

您似乎每次都在寻找四个记录。您可以使用窗口功能:

select t.*
from (select t.*,
             count(*) over (partition by id, datetime) as cnt
      from t
     ) t
where cnt = 4;

如果您不知道频道数,可以使用:

select t.*
from (select t.*,
             count(*) over (partition by id, datetime) as cnt,
             count(distinct channel) over () as num_channel
      from t
     ) t
where cnt = num_channel;

答案 1 :(得分:0)

尝试一下-

SELECT * FROM your_table
WHERE DateTime IN
(
    SELECT DateTime
    FROM your_table
    GROUP BY DateTime
    HAVING COUNT(DISTINCT Chanel) = 4
)