怎么用QUERY写汇总查询? (100分)

X

xmdsj

Unregistered / Unconfirmed
GUEST, unregistred user!
我用的是interbase数据库.其中有两个表.
TABLE1:XH(产品型号)、SL(产品总数量)
TABLE2:XM(姓名)、RQ(日期)、XH(产品型号)、SL(完成数量)

要求查询出产品的总数量及完成量(产品型号、产品总数量、完成数量)

 
一般可以这样写,但我对 interbase 不熟,试试看,可能要适当修改:
select a.xh,a.sl,b.sl wcl from table1 a,
(select xh,sl from table2 order by xh group by xh) b
where a.xh=b.xh
 
wcl是什么意思
 
改字段名,有的可能要在前面 加" As "
////////完成量
 
我试了一下还不行
我记得子查询好象不能包含在FROM语句中吧
 
select a.xh,a.sl,sum(b.sl) as wcl
from table1 a
join
table2 b
where a.xh=b.xh
 
好像Interbase 这个查询是不能用改字段名的,用AS也是不用了,不支持AS

我的意见是先改字段名再写查询,把总数量的字段名先改为ZSL
再写查询
你是否要统计完成量吗,还是查询详细的完成量记录
 
create view aa as select a.xh,a.sl,b.sl as zsl from table2 a,table1 b
where a.xh=b.xh

select xh,sum(sl) as sl,sum(zsl) as zsl from aa group by xh
 
select xh,sl zsl,(select sum(sl) from table2 where xh=AAA.xh) wcl
from table1 AAA
 
to 楼主:

我把子龙的代码改一下。
create view aa as select a.xh,a.sl,b.sl as zsl from table2 a,table1 b
where a.xh=b.xh
select * from aa

代码的意义就是从多个基本表中导出视图来,然后把这个视图中的所有纪录全部导出来。
 
谢谢大家
特别感谢QuickSilver
 
多人接受答案了。
 
顶部