不好意思,顺便补两个地方,代码应该是
select to_char(A.Logintime,'MM') as month,to_char(A.Logintime,'DD') as day,sum(A.Duration) as duration,avg(B.rate) as rate from A,B where A.UserID='05' and A.Logintime>=to_date('2005-1-1','yyyy-mm-dd hh24:mi:ss') and A.Logintime<to_date('2005-5-1','yyyy-mm-dd hh24:mi:ss') and A.UserID=B.UserID group by to_char(A.Logintime,'MM'),to_char(A.Logintime,'DD')
然后这个rate是取当天的平均值
select to_char(A.Logintime,'MM') as month,to_char(A.Logintime,'DD') as day,sum(A.Duration) as duration,avg(B.rate) as rate from A,B where A.UserID='05' and A.Logintime>=to_date('2005-1-1','yyyy-mm-dd hh24:mi:ss') and A.Logintime<to_date('2005-5-1','yyyy-mm-dd hh24:mi:ss') and A.UserID=B.UserID group by to_char(A.Logintime,'MM'),to_char(A.Logintime,'DD')
然后这个rate是取当天的平均值
b表不用增加日期字段了吧,不是跟a表通过userid连一起了么
--------
那你获取的Rate不是就是一个固定的值了吗?
2005-4-1 13:42:11 154
2005-4-1 13:42:12 168
2005-4-1 13:45:35 1242005-4-7 12:23:19 1472005-4-9 23:05:11 132
2005-4-9 23:56:12 145
就是类似这样的一个表,我想分别取出4-1,4-7,4-9这三天double的平均值
select avg(double) from table group by trunc(logintime)
首先,可以肯定,A表和B表是一对多的关系。如果去掉求平均值。SQL文我认为是没有问题的。也就是下面的
SQL文:
select to_char(A.Logintime,'MM') as month,to_char(A.Logintime,'DD') as day,sum(A.Duration) as duration from A where A.UserID='05' and A.Logintime>=to_date('2005-1-1','yyyy-mm-dd hh24:mi:ss') and A.Logintime<to_date('2005-5-1','yyyy-mm-dd hh24:mi:ss') and group by to_char(A.Logintime,'MM'),to_char(A.Logintime,'DD')
以上SQL文执行后得到了时间统计的结果。
下面我们再考虑求平均的rate.首先求得是一天的rate平均值。
那么,试问你怎样用一个简单的条件A.UserID=B.UserID就把从B表取得的rate限制在一天呢?!
因为B表存在和下面这样的数据。
B
5月1日的数据
Userid rate
USR01 10
USR01 12
USR01 16
5月2日的数据
Userid rate
USR01 11
USR01 19
USR01 17
试问,你又怎么可能用一个条件(A.UserID=B.UserID)来区分5月1日和5月1日的数据呢。
不知道我说明白没有?