日期:2014-05-17  浏览次数:20763 次

这东西能用子查询查出来吗?
现在有表结构及数据如下:

id      date                 number
1       2012-07-12 17:05:15  10
2       2012-07-12 18:12:15  12
3       2012-07-12 19:17:15  10
4       2012-07-12 20:26:15  19
5       2012-07-13 17:05:15  5
6       2012-07-13 18:05:15  12
7       2012-07-13 19:26:15  18
8       2012-07-13 20:29:15  23
9       2012-07-13 21:33:15  15

现在我想求12年7月份18点到20点的number总和,能用一条sql语句写出来么?需要几个子查询?求各位大神给小弟指点指点!
------解决方案--------------------
select sum(number) from t where to_number(to_char(date,'yyyymmhh24')) between 20120718 and 20120720

------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

select sum(number) from t where to_number(to_char(date,'yyyymmhh24')) between 20120718 and 20120720

楼上时间好像有点问题!
什么问题?

哈哈,是没有问题!
------解决方案--------------------
12年7月份18点到20点的number总和
建议使用to_char实现
to_char(date,'yyyy-mm')='2012-07'
to_char(date,'hh24')>=18 and to_char(date,'hh24')<=20
即:

select sum(number) from 表名 where to_char(date,'yyyy-mm')='2012-07' and 
to_char(date,'hh24')>=18 and to_char(date,'hh24')<=20