貌似简单的SQL查询,急切等待解决!!!

[复制链接]
查看11 | 回复9 | 2011-11-1 16:23:26 | 显示全部楼层 |阅读模式
table: tbl_task
_________________________________________________
TASKID NOT NULL NUMBER(10)
HOPID NOT NULL NUMBER(2)
BW
NUMBER(10)
NECKCONFINDEX NUMBER(1)

data:
________________________________________________
TASKIDHOPIDBW NECKCONFINDEX
---------- ---------- ---------- -------------
10001 100 1
10002 100 0
10001 400 0
10002 100 0
10002 300 1
10002 100 1
10001 100 0
10001 200 0
10003 100 1
10003 500 1
20002 100 0
20002 300 1
20002 100 1
20001 100 0
20001 200 0
...
要求:按TASKID,HOPID分组查询出BW平均值,以及NECKCONFINDEX=0所占当前组的百分比RATIO
____________________________________________________________________________
TASKIDHOPIDAVABW RATIO
---------- ---------- ---------- -------------
100012000.75
100021500.50
100033000
...
满足条件taskid=1000 and hopid=1的记录有4条,而满足taskid=1000 and hopid=1 and neckconfindex=0的记录有3条,那个百分比RATIO是3/4=0.75
回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层
学习一下。
回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层
Select a.taskid, a.hopid, a.avgBw, b.count0 / a.sumCount patio
from (select a.taskid, a.hopid, count(*) sumCount, avg(bw) avgBw

from tbl_task a
group by a.taskid, a.hopid) a,
(Select x.taskid, x.hopid, nvl(y.count0, 0) count0

from (select a.taskid, a.hopid

from tbl_task a

group by a.taskid, a.hopid) x,

(select a.taskid, a.hopid, count(*) count0

from tbl_task a

where a.neckconfindex = 0

group by a.taskid, a.hopid) y
where x.taskid = y.taskid(+)
and x.hopid = y.hopid(+)) b
where a.taskid = b.taskid
and a.hopid = b.hopid
回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层
原帖由 llkyq 于 2007-11-26 19:02 发表
Select a.taskid, a.hopid, a.avgBw, b.count0 / a.sumCount patio
from (select a.taskid, a.hopid, count(*) sumCount, avg(bw) avgBw

from tbl_task a
group by a.taskid, a.hopid) ...
佩服,不过好像把问题弄复杂了,直接用分析函数就可以了。


回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层
不用这么复杂吧??
select TASKID,HOPID,cast (count(case when neckconfindex=0 then 1 else 0) as decimal(10,2))/count(1)
group by TASKID,HOPID;
应该就可以了吧!
回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层
明天测试一把,应该可以实现的!
回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层
嗯,的确不复杂,关键楼主要给出测试数据。
回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层
select t.taskid,
t.hopid,
avg(bw),
count(case nci

when 0 then

1

else

null

end) / count(nci) as ratio
from task t
group by t.taskid, t.hopid;

这个可以
回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层
原帖由 zxnx 于 2007-11-26 21:32 发表
select t.taskid,
t.hopid,
avg(bw),
count(case nci

when 0 then

1

else

null

end) / count(nci) ...



回复

使用道具 举报

千问 | 2011-11-1 16:23:26 | 显示全部楼层

select t.taskid,

t.hopid,

avg(bw),

to_char(1- sum(nci) / count(nci), '0.00') as ratio
from task t
group by t.taskid, t.hopid;
....
复制代码
应该差不多啦


[ 本帖最后由 lixuezhenclone 于 2007-12-4 15:29 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行