1

我可以从下面的查询中获取每年的日期范围数据15/0221/06

SELECT * FROM dim_date
WHERE 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)

但问题是,当我想从 year 中查找时2010 - 2012,我得到了相同的结果。

我想得到的范围:

2010-02-15 - 2010-06-15
2011-02-15 - 2011-06-15
2012-02-15 - 2012-06-15

SELECT * FROM dim_date
WHERE 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)
AND EXTRACT (YEAR FROM date_cal) BETWEEN 2010 AND 2012

谁能帮我解决这个问题!

4

1 回答 1

3

您的问题是缺少括号:在 SQLAND中优先于OR,因此您需要在整个先前的表达式周围加上括号:

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

没有括号,你会得到A or B or (C and D),但你想要(A or B or C) and D

于 2012-08-15T03:08:48.013 回答