0

I have the answer below that worked fine for me:

now instead of selecting distinct or group by , I want to select all that records ext_no having value zero only.

How to modify this answer? I tried using order by but it's showing an error. Any help will be appreciated. Thanks in advance.

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

1 Answers1

1

There are a couple of ways you can achieve this. Using a couple of correlated subqueries in the WHERE clause or by JOINing to a table containing MAX(value) and COUNT(*) values for each value of ext_no:

Query 1

SELECT ext_no
FROM test t1
WHERE NOT EXISTS (SELECT * 
                  FROM test t2
                  WHERE t2.ext_no = t1.ext_no AND value != 0)
  AND (SELECT COUNT(*)
       FROM test t3
       WHERE t3.ext_no = t1.ext_no) > 2

The first subquery checks that this ext_no has no value other than 0. The second checks that there are more than 2 copies of this ext_no.

Query 2

In this query we apply all the conditions (no non-zero values of value and more than 2 copies of ext_no) in the JOIN condition.

SELECT t1.ext_no
FROM test t1
JOIN (SELECT ext_no, MAX(value) AS max_value, COUNT(*) AS count
      FROM test 
      GROUP BY ext_no) t2
  ON t2.ext_no = t1.ext_no AND t2.max_value = 0 AND t2.count > 2

Output (for both queries)

ext_no
12133
12133
12133
11505
11505
11505
11505

Demo on SQLFiddle

Nick
  • 138,499
  • 22
  • 57
  • 95
  • no.. it selects all value having zero.. the condition is ..if count of ext_no is more than two.. and again if and only if all that ext_no is zero then select ...only.. please have a look [link](https://stackoverflow.com/questions/52840268/mysql-query-to-select-distinct-records-on-conditions/52840568#52840568) – Sachin_Bhagat_Ssbits Nov 17 '18 at 03:01
  • @user10146017 See my edit. I hope that I have understood your question correctly this time. – Nick Nov 17 '18 at 03:25
  • yes i am giving it a try..actual database is having more than 200k records so its taking time may be.. – Sachin_Bhagat_Ssbits Nov 17 '18 at 03:42
  • @user10146017 you will probably find the second version of the query is much faster as in the first query the correlated subquery has to be run for each row of the table – Nick Nov 17 '18 at 03:44
  • yes exactly..that second work superb..thank you for this.. :) , what you suggest for updating those records.. – Sachin_Bhagat_Ssbits Nov 17 '18 at 03:51
  • @user10146017 Glad to hear it. I think updating the values belongs in a new question. – Nick Nov 17 '18 at 03:55
  • yes.sure..in next question ..i mean if i want to delete those records..would be. delete from test where..below your query...please correct if i am wrong.. – Sachin_Bhagat_Ssbits Nov 17 '18 at 04:32
  • 1
    @user10146017 if you just want to delete you can use the answer from your previous question, `DELETE FROM test WHERE ext_no IN (...)` – Nick Nov 17 '18 at 04:34
  • it shows #1241 - Operand should contain 1 column(s) – Sachin_Bhagat_Ssbits Nov 18 '18 at 03:52
  • Just remove the `, MAX(value)` from the query – Nick Nov 18 '18 at 04:07