我通过这个sql查询我想计算05:00:00 PM
之后和之前的记录
05:00:00 PM
。在这里我查询但是通过这个我得到错误的结果请任何人检查这个如果有任何逻辑错误
Select trunc(gross_weight_date) date1,
count(*) before5
from wbg.WBG_01_01
where to_char(gross_weight_date,'HH:MI:SS PM')>'05:00:00 PM'
and item_cod = 16
and trunc(gross_weight_date)='05-JAN-2012'
group by trunc(gross_weight_date)
order by date1
可以提供任何帮助
答案 0 :(得分:3)
您正在使用chars
来比较日期。这将无法正常工作。
由于您只想在05 PM之后进行比较,因此这是一个更简单的解决方案:
Select trunc(gross_weight_date) date1,
count(*) before5
from wbg.WBG_01_01
where to_number(to_char(gross_weight_date, 'HH24MISS')) > 150000
and item_cod = 16
and trunc(gross_weight_date)='05-JAN-2012'
group by trunc(gross_weight_date)
order by date1
它将日期的值传递给char('171212'),然后传递给数字(171212)并将其与150000进行比较。
答案 1 :(得分:3)
trunc(gross_weight_date)
是日期,而'05-JAN-2012'
是VARCHAR2)。使用日期时,您可以使用日期函数和日期算术而无需转换,例如:
Select trunc(gross_weight_date) date1, count(*) before5
from wbg.WBG_01_01
where item_cod = 16
and gross_weight_date > to_date('05-01-2012', 'DD-MM-YYYY') + 17/24
and gross_weight_date < to_date('05-01-2012', 'DD-MM-YYYY') + 1
group by trunc(gross_weight_date)
order by date1
或
Select trunc(gross_weight_date) date1, count(*) before5
from wbg.WBG_01_01
where item_cod = 16
and gross_weight_date > to_date('05-01-2012', 'DD-MM-YYYY')
+ numtodsinterval(17, 'HOUR')
and gross_weight_date < to_date('05-01-2012', 'DD-MM-YYYY')
+ numtodsinterval(1, 'DAY')
group by trunc(gross_weight_date)
order by date1