Mysql query to select Distinct Records on conditions?

夙愿已清 提交于 2019-12-02 08:51:17

count skips null values. So you can count a case expression where the value is 0, and then use a having condition to check that this count is equal to the total count:

SELECT   ext_no, MAX(value)
FROM     test
GROUP BY ext_no
HAVING   COUNT(*) > 2 AND 
         COUNT(*) = COUNT(CASE value WHEN 0 THEN 1 END)

You can try this one, using group by and the having clause:

SELECT
    ext_no,
    SUM(value) as value
FROM
    test
GROUP BY
    ext_no
HAVING
    count(*) > 2 AND SUM(value) = 0

Try this

SELECT t.ext_no, COUNT(*)
FROM test t
WHERE NOT EXISTS (
    SELECT 1
    FROM test
    WHERE ext_no = t.ext_no AND ext_no > 0
)
GROUP BY t.ext_no
HAVING COUNT(*) > 2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!