SQL仅获取列中具有所有值的项目

时间:2019-04-10 20:29:42

标签: sql postgresql window-functions

我有这样的sql查询:

sd()

我得到这些行: enter image description here

我的问题是,我只需要这些id,在所有e_end_time列中都是时间戳。对于此示例,它仅是id2。有人可以帮助我,怎么做?

Id 1有时间戳,但仅在第一行。第二行和第三行的e_end_time列为空,这就是为什么不应该将其返回的原因。

4 个答案:

答案 0 :(得分:1)

在您的WHERE子句中添加NOT IN子查询

WHERE m.is_deleted is FALSE
  AND m.id NOT IN (SELECT DISTINCT monitor_id 
                   FROM extractions
                   WHERE end_time IS NULL)

答案 1 :(得分:1)

将此内容添加到where子句中应删除结束时间为空的ID

and m.id not in (select id from monitors where e_end_time is null)
SELECT m.id, 
e.id AS extraction_id, 
row_number() OVER (PARTITION BY m.id ORDER BY e.start_time DESC) as 
   start_date_rank,
e.end_time AS e_end_time, 
e.status AS e_status
FROM monitors m 
LEFT JOIN extractions e 
ON m.id = e.monitor_id 
WHERE m.is_deleted is FALSE
and m.id not in (select id from monitors where e_end_time is null)

答案 2 :(得分:0)

您可以只使用group by

SELECT m.id
FROM monitors m LEFT JOIN
     extractions e 
     ON m.id = e.monitor_id 
WHERE m.is_deleted is FALSE
GROUP BY m.id
HAVING COUNT(*) = COUNT(e.end_time);

答案 3 :(得分:0)

SELECT m.id, 
e.id AS extraction_id, 
row_number() OVER (PARTITION BY m.id ORDER BY e.start_time DESC) as 
   start_date_rank,
e.end_time AS e_end_time, 
e.status AS e_status
FROM monitors m 
LEFT JOIN extractions e 
ON m.id = e.monitor_id 
WHERE m.is_deleted is FALSE AND DATALENGTH(e.end_time)<>0

DATALENGTH()函数返回用于表示表达式的字节数。

DataLength是解决您问题的最好方法,因为它还会删除您的空白...

谢谢!