2017-08-05 2 views
-1

J'ai deux tables: La table T1 référence l'ID du tableau T2.Vérification SQL si la date du Tableau 1 est comprise entre la plage du Tableau 2

T1 
|id_t2|start_date| 
|00001|2002-01-01| 
|00001|2003-01-01| 
|00001|2004-01-01| 
|00001|2010-01-01| 
|00002|2002-01-01| 
|00002|2003-01-01| 
|00002|2004-01-01| 

T2 
|id_t2|start_date| end_date | 
|00001|2002-08-01|2002-12-31| 
|00001|2003-01-01|2006-01-01| 
|00002|2002-02-01|2002-12-31| 
|00002|2003-01-01|2006-01-01| 

Expected résult: 
|00001|2002-01-01| <= There is no line on T2 where ids are the same and date from T1 is between T2 start_dab and end_date. 
|00001|2010-01-01| <= There is no line on T2 where ids are the same and date from T1 is between T2 start_dab and end_date. 
+1

Qu'avez-vous réellement vous essayé? –

Répondre

0

Vous pouvez utiliser l'opérateur not exists:

SELECT * 
FROM t1 
WHERE NOT EXISTS (SELECT * 
        FROM t2 
        WHERE t2.id_t2 = t1.id_t2 AND 
          t1.start_date BETWEEN t2.start_date and t2.end_date) 
+0

ça a l'air bien. thk – Kagwalmina