我正在尝试搜索结构如下的记录
trans_Date, customer_age, etc
其中客户年龄是交易时的年龄。我需要做的是选择当前特定年龄的所有客户,他们在日期范围之间进行了交易。
假设我只想要在 2005 年至 2010 年之间购买的当前 20 岁的人
我想出了这个
select * from trans_logs
where trans_date between TO_DATE("07/13/2005:00:00","MM/DD/YYYY:HH24:MI" and TO_DATE("01/01/2010:00:00","MM/DD/YYYY:HH24:MI")
and customer_age between 13 and 18
我得到了一些正确的结果,但我也得到了 2005 年 18 岁时进行交易的人(现在他们 25 岁)
and((customer_age=13 and trans_date BETWEEN TO_DATE('01-JAN-2005:00:00'.'DD-MON-YYYY:HH24:MI') and TO_DATE('31-DEC-2005:23:59','DD-MON-YYYY:HH24:MI'))
or (customer_age=14 and trans_date BETWEEN TO_DATE('01-JAN-2006:00:00'.'DD-MON-YYYY:HH24:MI') and TO_DATE('31-DEC-2006:23:59','DD-MON-YYYY:HH24:MI'))
or (customer_age=15 and trans_date BETWEEN TO_DATE('01-JAN-2007:00:00'.'DD-MON-YYYY:HH24:MI') and TO_DATE('31-DEC-2007:23:59','DD-MON-YYYY:HH24:MI'))
or (customer_age=16 and trans_date BETWEEN TO_DATE('01-JAN-2008:00:00'.'DD-MON-YYYY:HH24:MI') and TO_DATE('31-DEC-2008:23:59','DD-MON-YYYY:HH24:MI'))
or (customer_age=17 and trans_date BETWEEN TO_DATE('01-JAN-2009:00:00'.'DD-MON-YYYY:HH24:MI') and TO_DATE('31-DEC-2009:23:59','DD-MON-YYYY:HH24:MI'))
or (customer_age=18 and trans_date BETWEEN TO_DATE('01-JAN-2010:00:00'.'DD-MON-YYYY:HH24:MI') and TO_DATE('31-DEC-2010:23:59','DD-MON-YYYY:HH24:MI')) )
但这会返回一个错误,说我缺少正确的括号。1. 我错过了什么?和 2. 有没有更有效的方法来做到这一点?
编辑:忘记了第二个日期和其他一些格式化的东西