数据表如下:
| ID |
时间 |
字段1 |
字段2 |
字段3 |
| 1 |
2006-11-2 1:10:00 |
001 |
60 |
30 |
| 2 |
2006-11-2 1:30:00 |
001 |
50 |
20 |
| 3 |
2006-11-2 1:10:00 |
002 |
50 |
10 |
| 4 |
2006-11-2 1:30:00 |
002 |
43 |
7 |
| 5 |
2006-11-2 2:15:00 |
001 |
80 |
10 |
| 6 |
2006-11-2 2:15:00 |
002 |
75 |
5 |
|
... |
... |
... |
... |
| 23 |
2006-11-2 23:00:00 |
001 |
40 |
10 |
数据表描述:
1.字段1只可能是001和002,且每一时段字段1的数据为字段2和字段3的数据之和。
即 时间=2006-11-2 1:00:00时,001的数据为60+30,002的数据为50+10
2.每一时段002的数据来之001的字段2的数据。
即 时间=2006-11-2 1:00:00时,001的数据(50+10)=该时刻002的字段2的数据 60
报表如下:
| 时间段 |
字段2 |
字段3 |
比率 |
| 01:00:00-01:59:59 |
60 |
10 |
67/160 |
| 02:00:00-02:59:59 |
50 |
7 |
|
| ... |
|
|
|
问题描述:
1.以1小时为间隔统计一天中001的字段2和002的字段3
2.某一个小时内,比率=(001的字段3+002的字段3)/001的字段2+001的字段3
即2006-11-2 1点这个小时内的比率=((30+20)+(10+7))/(60+50+30+20)
SQL语句:

SQL语句
select cast(时间 as varchar)+':00:00' as 时间,
字段2_001 as 字段2 ,
字段3_002 as 字段3,
case when (字段2_001+字段3_001)=0 then null else (字段3_001+字段3_002)/(字段2_001+字段3_001) end as 比率,
hc as 总呼出
from
(SELECT time_002,
SUM(字段2) AS 字段2_002,
SUM(字段3) AS 字段3_002,
FROM
(SELECT DATEPART(hour, 时间) AS time_002,
字段2_002,
字段3_002
FROM 表1
WHERE (字段1 = 002) and time between '2006-11-1 01:00:00' and '2006-11-1 23:59:00') 02
GROUP BY time_002) 002,
(SELECT time_001,
SUM(字段2) AS 字段2_001,
SUM(字段3) AS 字段3_001,
FROM
(SELECT DATEPART(hour, 时间) AS time_001,
字段2_001,
字段3_001
FROM 表1
WHERE (字段1 = 001) and time between '2006-11-1 01:00:00' and '2006-11-1 23:59:00') 01
GROUP BY time_001) 001
where 002.time_002=001.time_001
order by time_002 asc
go

欢迎感兴趣的朋友一起讨论!