ok,我把前两天的问题重贴一遍吧
table1中记录为:
code name kind money
01 corp1 01 100
01 corp1 02 150
02 corp2 01 200
table2中记录为
code name kind money
01 corp1 01 150
01 corp1 02 200
01 corp1 03 100
02 corp2 01 300
03 corp3 01 400
目的是统计相同code(name)记录的money总和和增长,放在同一个表里
开始写的代码如下
select table1.Code, table2.Name, sum(table1.money), sum(table2.money), (sum(table1.money)-sum(table2.money))/sum(table2.money)
from table1 join table2 on table1.code=table2.code
group by table1.Code, table1.Name
发现有两个问题
1.重复计算问题:该程序计算code为01的结果是实际的几倍(code2正常),可是,分别计算table1和table2的sum(money)时
结果是正确的,怀疑是 table1.code=table2.code该语句造成的,可不知怎么改
2.对于table2中的新code,我想体现在新表中,有什么方法可以实现
有两位大侠给了程序
select a.code,a.name,sum(a.money),sum(b.money),sum(a.money)-sum(b.money) from
(select Code, Name, sum(money) money from table1 group by code, name) a,
(select Code, Name, sum(money) money from table2 group by code, name) b
where a.code=b.code
group by a.code,a.name
和
select code,name,sum(money1) ,sum(money2),(sum(money1)-sum(money2))/sum(money2)
from
(
select table1.code,table1.name,table1.money as money1,0 as money2
from table1
union all
select table2.code,table2.name,0 as money1,table2.money as money2
from table2
) as a
group by code,name
在sql server 下运行都没大问题
当我移到Tquery组件下却调不通
提示为嵌套中的select 出错
不知何故