79804395

Date: 2025-10-30 04:38:36
Score: 1
Natty:
Report link

Thank you Charlieface, for the query, you gave me what I wanted. I altered a bit to get the results I wanted. I have to test this further, and this is just the part of the problem. I will have to check this with entire query and test it. This is just 1 code, in reality there are 5 types of code in table t1 with individual include and exclude conditions in ranges for each of the codes. I believe this is what I wanted:

SELECT *
FROM table1 t1
Left Join table2 t2_x on t1.Code between t2_x.FromCode and t2_x.ToCode and t2_x.IsExcluded = 1
WHERE t2_x.ID is NULL 
    AND NOT EXISTS (SELECT 1
    FROM table2 t2
    WHERE t2.IsExcluded = 0)

UNION ALL

SELECT *
FROM table1 t1
WHERE EXISTS (SELECT 1
  FROM table2 t2
  WHERE t1.Code BETWEEN t2.FromCode AND t2.ToCode
  AND t2.IsExcluded = 0)
AND NOT EXISTS (SELECT 1
    FROM table2 t2_x
    WHERE t1.Code BETWEEN t2_x.FromCode AND t2_x.ToCode
    AND t2_x.IsExcluded = 1);
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Amit Nerurkar