关于sql命令在delphi下不能运行的问题(50分)

  • 主题发起人 主题发起人 cosmile
  • 开始时间 开始时间
C

cosmile

Unregistered / Unconfirmed
GUEST, unregistred user!
在sqlserver下程序运行正常,但在delphi的Tquery控件下,运行提示格式错误
不知道要正常运行需要如何做
请大家帮忙想一下。
 
说说你的SQL语句先
 
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 出错
不知何故

 
在sqlserver下建立视图 TABLE_VIEW
( 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)

运行query
(select code,name,sum(money1) ,sum(money2),(sum(money1)-sum(money2))/sum(money2)
from TABLE_VIEW
group by code,name)

OK!
 
我用delphi时要脱离sql server

所以不能用sqlserver来建视图

该怎么办啊
 
或者用QUERY建立临时表
create table #table (....)
insert into #table (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)

再用QUERY输出
(select code,name,sum(money1) ,sum(money2),(sum(money1)-sum(money2))/sum(money2)
from #table
group by code,name)


 
接受答案了.
 
后退
顶部