如果至少满足一个条件,则消除所有标识符

时间:2019-05-10 17:59:41

标签: mysql sql

我有三列:标识符,driver_id,driver_id_was

我的条件是消除那些始终具有driver_id和driver_ids_was为-1的标识符。

但是我当前的查询却相反,它包括所有具有driver_id和driver_id_was至少为-1的标识符。

SELECT identifier
        ,model
        ,sw_pkg_version
        ,COUNT(CASE WHEN driver_id_was != -1 THEN 1 ELSE NULL END) AS "count_driver_id_was_not"
        ,COUNT(CASE WHEN driver_id_was = -1 THEN 1 ELSE NULL END) AS "count_driver_id_was"
        ,COUNT(CA

 FROM eld_messages
 WHERE model LIKE '%ca'
       AND created_at > getdate()-30
 GROUP BY identifier
          ,driver_id_was
          ,model
          ,sw_pkg_version
          ,driver_id
 HAVING count_driver_id_was_not = 0
    AND count_driver_id_was > 0;

2 个答案:

答案 0 :(得分:0)

  

我的条件是消除那些始终具有driver_id和driver_ids_was为-1的标识符。

这是您想要的吗?

select identifier
from eld_messages
group by identifier
having sum(case when count_driver_id_was_not <> -1 then 1 else 0 end) > 0 or
       sum(case when count_driver_id_was <> -1 then 1 else 0 end) > 0;

或者,类似但更简洁:

select distinct identifier
from eld_messages
where count_driver_id_was_not <> -1 or count_driver_id_was <> -1

答案 1 :(得分:0)

这就是我对问题的理解,但是如果没有示例数据或进一步说明,很难确定。也许这个答案会产生一些反应。

SELECT identifier
    ,model
    ,sw_pkg_version
FROM eld_messages
WHERE model LIKE '%ca'
  AND created_at > getdate()-30
  AND driver_id <> -1
  AND driver_id_was <> -1