现在写一个存储过程
查询出的数据如下,放在一个游标中,
DAY CNT CODE
1 21 435 DST
2 22 95 DST
3 20 13 DST
4 21 343 GS
5 22 97 GS
6 20 22 GS
7 21 675 HFE
8 22 161 HFE
9 20 36 HFE
10 21 378 KHN
11 22 73 KHN
12 20 5 KHN
因为要对每一个CODE取CNT的最大值,最大值的DAY和CNT的平均值。 如DST应该取
maxDAY,maxCNT,avaCNT,code
21 435 181 DST
不知道用什么SQL读入游标完成。所以现在想建个临时表,然后将数据写入到临时表。之后将临时表的数据读入游标。
但考虑到一个问题,如果有两个用户同时调用该存储过程,那么建临时表 CREATE GLOBAL TEMPORARY TABLE temp不是会冲突吗?望大家帮帮小弟忙~谢谢
查询出的数据如下,放在一个游标中,
DAY CNT CODE
1 21 435 DST
2 22 95 DST
3 20 13 DST
4 21 343 GS
5 22 97 GS
6 20 22 GS
7 21 675 HFE
8 22 161 HFE
9 20 36 HFE
10 21 378 KHN
11 22 73 KHN
12 20 5 KHN
因为要对每一个CODE取CNT的最大值,最大值的DAY和CNT的平均值。 如DST应该取
maxDAY,maxCNT,avaCNT,code
21 435 181 DST
不知道用什么SQL读入游标完成。所以现在想建个临时表,然后将数据写入到临时表。之后将临时表的数据读入游标。
但考虑到一个问题,如果有两个用户同时调用该存储过程,那么建临时表 CREATE GLOBAL TEMPORARY TABLE temp不是会冲突吗?望大家帮帮小弟忙~谢谢
有什么特殊需求?查询出的数据如下,放在一个游标中(很奇怪,直接在外层在统计不行?),
DAY CNT CODE
1 21 435 DST
2 22 95 DST
3 20 13 DST
4 21 343 GS
5 22 97 GS
6 20 22 GS
7 21 675 HFE
8 22 161 HFE
9 20 36 HFE
10 21 378 KHN
11 22 73 KHN
12 20 5 KHN
因为要对每一个CODE取CNT的最大值,最大值的DAY和CNT的平均值
where pass_time between '20100420000000' and '20100425235900'
and point_code in ('HFE','MADUK','TOL','XLN','DST','GS','VMB','PLT','KHN','UDINO')
and fp24.sea_flag = 'A'
group by point_code,SUBSTR(pass_time, 7, 2)
order by point_code, cnt desc
这个SQL得出以上数据。
point_code,pass_time,fp05_id字段类型都是varchar
要对每一个CODE取其最大值,和每一个CODE的平均值。比如以上数据应该统计出的结果为
maxDAY,maxCNT,avaCNT,code
21 435 181 DST
21 343 154 GS
21 675 290 HFE
21 378 152 KHN
如何得到统计数据呢?
from (
--你的SQL
select SUBSTR(pass_time, 7, 2) as day, count(distinct(fp05_id)) as cnt,point_code as code from fp24_his fp24
where pass_time between '20100420000000' and '20100425235900'
and point_code in ('HFE','MADUK','TOL','XLN','DST','GS','VMB','PLT','KHN','UDINO')
and fp24.sea_flag = 'A'
group by point_code,SUBSTR(pass_time, 7, 2)
order by point_code, cnt desc
)
group by code
先建个视图:
create or replace view testone as
(
-- 你的sql
)
先判断cnt是最大的,如果cnt有相同的值,再判断day是相同cnt中最大的。
select max(day),cnt,code from
(testone) va
where cnt in (select max(cnt) from (testone) vb where va.code = vb.code)
group by code,cnt
select max(day),max(cnt),(select trunc(avg(cnt)) from testone vc where va.code = vc.CODE) as vagcnt,code from
(testone) va
where cnt in (select distinct max(cnt) from (testone) vb where va.code = vb.code)
group by code
from table_name a,
( select CODE, max(CNT) CNT_max , avg(CNT) CNT_avg
from table_name
group by CODE
) b
where b.CODE = a.CODE
from table_name a,
( select CODE, max(CNT) CNT_max , avg(CNT) CNT_avg
from table_name
group by CODE
) b
where b.CODE = a.CODE
group by b.CODE, b.CNT_max, b.CNT_avg
select b.CODE, b.CNT_max, b.CNT_avg, a.DAY
from table_name a,
( select CODE, max(CNT) CNT_max , avg(CNT) CNT_avg
from table_name
group by CODE
) b
where b.CODE = a.CODE and b.CNT_max = a.CNT
上面SQL只是取了day的最大值.