sql >> Database teknologi >  >> RDS >> Mysql

SQL-forespørgsel for GROUP BY, returnerer grupper, der matcher betingelserne for LEAST, COALESCE

Få først tour_id s, der matcher betingelserne:

SELECT Tour_ID
FROM myTable 
WHERE organisation LIKE 'Ikea'
GROUP BY Tour_ID
HAVING LEAST(COALESCE(MIN(A), '2019-01-01'), COALESCE(MIN(B), '2019-01-01'), COALESCE(MIN(C), '2019-01-01'), COALESCE(MIN(D), '2019-01-01')) >= '2018-05-01' AND
       LEAST(COALESCE(MIN(A), '2019-01-01'), COALESCE(MIN(B), '2019-01-01'), COALESCE(MIN(C), '2019-01-01'), COALESCE(MIN(D), '2019-01-01')) < '2018-06-01';

Indsæt derefter dette i en forespørgsel for at få de originale rækker. Her er én måde:

select t.*
from mytable t join
     (SELECT organisation, Tour_ID
      FROM myTable 
      WHERE organisation LIKE 'Ikea'
      GROUP BY organisation, Tour_ID
      HAVING LEAST(COALESCE(MIN(A), '2019-01-01'), COALESCE(MIN(B), '2019-01-01'), COALESCE(MIN(C), '2019-01-01'), COALESCE(MIN(D), '2019-01-01')) >= '2018-05-01' AND
             LEAST(COALESCE(MIN(A), '2019-01-01'), COALESCE(MIN(B), '2019-01-01'), COALESCE(MIN(C), '2019-01-01'), COALESCE(MIN(D), '2019-01-01')) < '2018-06-01'
     ) tt
     ON tt.tour_id = t.tour_id AND
        tt.organisation = t.organisation;



  1. MySQL - At finde tid overlapper

  2. Bind adresse og MySQL-server

  3. Hvordan får man tabeldefinition i Oracle?

  4. SQL Ydeevne UNION vs. OR