Masalah Anda adalah kurangnya tanda kurung:Dalam SQL AND
diutamakan daripada OR
, jadi Anda perlu memberi tanda kurung di sekitar ekspresi Anda sebelumnya:
SELECT * FROM dim_date
WHERE ( -- added bracket
EXTRACT (MONTH FROM date_cal) BETWEEN 3 AND 5
OR (EXTRACT (MONTH FROM date_cal) = 2 AND EXTRACT (DAY FROM date_cal) >= 15)
OR (EXTRACT (MONTH FROM date_cal) = 6 AND EXTRACT (DAY FROM date_cal) <= 21)
) -- added bracket
AND EXTRACT (YEAR FROM date_cal) BETWEEN 2010 AND 2012
Tanpa tanda kurung, Anda mendapatkan A or B or (C and D)
, tetapi Anda ingin (A or B or C) and D