关于SQL查询统计的问题!希望大家帮帮忙!(敬送100分)(100分)

  • 主题发起人 主题发起人 yyzyyz123
  • 开始时间 开始时间
Y

yyzyyz123

Unregistered / Unconfirmed
GUEST, unregistred user!
[yellow][/yellow]有一个数据表:字段为:XH(序号),BM(部门),NIAN(年),YUE(月),
ZB(洲别),LXZL(来信种类),LXSL(来信数量),GJDQ(国家地区)等几个字段
现在想按部门统计(1-12个月的来信数量以及全年的来信数量)放入另一个表中
(字段为BM(部门),1月来信数,2月来信数,3月来信数,4月来信数,5月来信数,6月来信数,7月来信数,
8月来信数,9月来信数,10月来信数,11月来信数,12月来信数,全年总和)
希望知道如何用DELPHI5的SQL语句如何实现,请说的详细一点,我是个初学者,拜托了!
谢谢!
 
分兩步:
select bm,
1月=(select isnull(sum(lxsl),0) from table1 a where a.bm=table1.bm and yue=1 and nian=:year),
2月=(select isnull(sum(lxsl),0) from table1 a where a.bm=table1.bm and yue=2 and nian=:year),
..
12月=(select isnull(sum(lxsl),0) from table1 a where a.bm=table1.bm and yue=12 and nian=:year),
into table2
from Table1 group by bm
update table2 set 全年=(1月+2月+.....+12月)
這樣table2應為你要的結果
 
select bm,sum(case yue when 1 then lxsl else 0 end) as m1,
sum(case yue when 2 then lxsl else 0 end) as m2,
..........
m1+m2+m3+m4=...
from Table1
group by bm
where nian=:nian


insert into table2 select * from 上表结果
insert into table2 select * from(
select bm,sum(case yue when 1 then lxsl else 0 end) as m1,
sum(case yue when 2 then lxsl else 0 end) as m2,
..........
m1+m2+m3+m4=...
from Table1
group by bm
where nian=:nian )

 
如果您是泉州人,打我电话:13505971722
我去上门辅导!
 
请问在DELPHI下用QUERY可以运行吗?
我不是很懂![:(]
 
ugvanxk你好,斗胆向你建议,你的写法是不是有点麻烦了,有没有简单一点的方法。
小弟在此有礼了。
 
多人接受答案了。
 
后退
顶部