一个有难度的sql
有3张表,
test1
id,docdate
test2
id,rowno,area,type1
test3
id,value,type2
其中表test1是主表,test2和test3是test1的子表
现在要创建一个view,
docdate,sum(area),sum(value) ;
要求必须一天一条纪录;
create view vwtest as select docdate,sum(area) area,sum(value) value from test1 a,test2 b,test3 c where a.id=b.id and a.id=c.id and b.type1='a' and c.type2='b' group by docdate;
可是创建的view不止一天一条纪录,而是很多条;
用下面的方法创建可以实现一天一条,
create view vwtest as select docdate,sum(area) area,sum(value) value from (select docdate,sum(area) area,sum(value) value from test1 a,test2 b,test3 c where a.id=b.id and a.id=c.id and b.type1='a' and c.type2='b' group by docdate ) aa group by docdate;
希望有个效率更高的sql
================================================================
★★ chen_cyh(逐波流) ●
问题点数:20、回复次数:2Top
1 楼KingSunSha(弱水三千)回复于 2002-08-06 23:29:50 得分 20
第一条sql语句和第二条的执行结果应该是一模一样的!
如果只是第一条语句不合要求,每天多过一条记录的话,我还能猜测是因为docdate中包含了时间字段,同一天中有多个时间不同的值,那样用trunc()函数过滤掉时间值就可以了。如下:
select trunc(docdate) docdate,sum(area) area,sum(value) value
from test1 a,test2 b,test3 c
where a.id=b.id
and a.id=c.id
and b.type1='a'
and c.type2='b'
group by trunc(docdate);
但是无论如何想不通为什么同样的情况下第二条可以,奇怪之极!
Top
2 楼chen_cyh(figer)回复于 2002-08-21 16:51:19 得分 0
多谢,我找到原因了Top




