请大家快帮帮忙,数据库的问题,愁死我了! (200分)

  • 主题发起人 主题发起人 天崖无爱
  • 开始时间 开始时间

天崖无爱

Unregistered / Unconfirmed
GUEST, unregistred user!
select htd_bgxsrhz.kmdm as 科目代码,tc_gyskm.kmmc as 科目名称,htd_bgxsrhz.ylj as 汇总金额 from htd_bgxsrhz,htd_bgxtkhz,tc_gyskm where tc_gyskm.kmdm=htd_bgxsrhz.kmdm and htd_bgxsrhz.yszl=tc_gyskm.szbz having htd_bgxsrhz.bjje=0 and htd_bgxsrhz.rlj<>0 and htd_bgxsrhz.xlj<>0 and htd_bgxtkhz.bjje<>0 and htd_bgxtkhz.rlj<>0 and htd_bgxtkhz.xlj<>0
运行以后我得到下面的表
kmdm kmmc ylj
010101 科目一 700
010102 科目二 100
在上面的语句中加入什么语句才能得到以下的结果
kmdm kmmc ylj
010101 科目一 600
010102 科目二 100
条件就是用原表中的第一条记录减下面这个表的第一条记录
kmdm kmmc ylj
010101 科目一 100
 
你是不是搞错了,你的结果好象是yh1.rlj-yh2.rlj
 
select a.kmdm,a.kmmc,a.rlj-isnull(b.rlj,0) rlj into yh from yh1 a,yh2 b
where a.kmdm=*b.kmdm
 
select kmdm,kmmc,rlj-(select sum(rlj) from yh2 where yh2.kmdm=yh1.kmdm) from yh1
 
select a.rlj-b.rlj from
(select * from yh1) a left join
(select * from yh2) b on a.kmdm=b.kmdm
 
对,差不多就是yh1-yh2用sql怎么样实现?
 
select kmdm,kmmc,yh1.rlj-yh2.rhl.yh2 as rlj
from yh1,yh2
where yh1.rlj=yh2.rlj
 
select kmdm,kmmc,rlj-(select sum(rlj) from yh2 where yh2.kmdm=yh1.kmdm) from yh1
 
insert into yh(kmdm,kmmc,rlj) select * from
(
select a.kmdm,a.kmmc,sum(a.rlj)-sum(isnull(b.rlj,0))
from yh1 a left join yh2 b
on a.kmdm=b.kmdm
group by a.kmdm,a.kmmc
) tablename
 
to qianwt
为什么你的程序把每一个数都减了,有一个不用减的啊
 
我同意jsxjd/leezero的意见
 
来不及想个简练的 虽然晦涩 但是可以实现

select AAA.*, BBB.kmmc
from
(select kmdm,sum(rlj) from
(select kmdm, rlj from yh1
union
select kmdm, rlj*(-1) from yh2 )
group by kmdm) AAA, yh1 BBB
where AAA.kmdm = BBB.kmdm
 
原来你就在网上 看看吧 10分钟后 我下网
 
to 天崖无爱:
你用我的SQL语句执行肯定可以,因为我用了外关联,减掉哪个是NULL,我已经把它转化成0了
 
怎么你们给我的都是减一个数啊?
得到的解果大多都是
表yh
kmdm kmmc rlj
010101 科目名称1 600
010102 科目名称2 0
010103 科目名称3 100
 
select htd_bgxsrhz.kmdm as 科目代码,tc_gyskm.kmmc as 科目名称,htd_bgxsrhz.ylj-(select sum(htd_bgxtkhz.rlj) from htd_bgxsrhz,htd_bgxtkhz where htd_bgxsrhz.skgkdm=htd_bgxtkhz.skgkdm and htd_bgxtkhz.kmdm=htd_bgxsrhz.kmdm and htd_bgxtkhz.zwrq=htd_bgxsrhz.zwrq and htd_bgxtkhz.jgdm=htd_bgxsrhz.jgdm group by htd_bgxtkhz.skgkdm having htd_bgxtkhz.bjje<>0 and htd_bgxtkhz.rlj<>0 and htd_bgxtkhz.xlj<>0) as 汇总金额,htd_bgxtkhz.ylj from htd_bgxsrhz,htd_bgxtkhz,tc_gyskm where tc_gyskm.kmdm=htd_bgxsrhz.kmdm and htd_bgxsrhz.yszl=tc_gyskm.szbz having htd_bgxsrhz.bjje=0 and htd_bgxsrhz.rlj<>0 and htd_bgxsrhz.xlj<>0 and htd_bgxtkhz.bjje<>0 and htd_bgxtkhz.rlj<>0 and htd_bgxtkhz.xlj<>0
帮我看看这条语句怎么样改才能实现上面的功能,就是让yh1-yh2
 
用这个试试
select a.kmdm,b.kmmc a.rlj-b.rlj into ry2 from ry1 as a ,ry3 as b where
a.kmdm=b.kmdm and (a.rly>b.rlj)
 
insert into yh
select kmdm,kmmc,rlj-isnull((select sum(rlj) from yh2 where yh2.kmdm=yh1.kmdm and yh2.kmmc=yh1.kmc),0) from yh1
 
select htd_bgxsrhz.kmdm as 科目代码,tc_gyskm.kmmc as 科目名称,htd_bgxsrhz.ylj as 汇总金额 from htd_bgxsrhz,htd_bgxtkhz,tc_gyskm where tc_gyskm.kmdm=htd_bgxsrhz.kmdm and htd_bgxsrhz.yszl=tc_gyskm.szbz having htd_bgxsrhz.bjje=0 and htd_bgxsrhz.rlj<>0 and htd_bgxsrhz.xlj<>0 and htd_bgxtkhz.bjje<>0 and htd_bgxtkhz.rlj<>0 and htd_bgxtkhz.xlj<>0
运行以后我得到下面的表
kmdm kmmc ylj
010101 科目一 700
010102 科目二 100
在上面的语句中加入什么语句才能得到以下的结果
kmdm kmmc ylj
010101 科目一 600
010102 科目二 100
条件就是用原表中的第一条记录减下面这个表的第一条记录
kmdm kmmc ylj
010101 科目一 100
 
Select kmdm,kmmc,Sum(rlj) as rlj
From
(Select kmdm,kmmc,rlj From yh1
union all
Select kmdm,kmmc,-rlj as rlj From yh2) AA
Group by kmdm,kmmc
 
后退
顶部