挑战 SQL高手。 在csdn没人做得来。(0分)

  • 主题发起人 编程傻子
  • 开始时间

编程傻子

Unregistered / Unconfirmed
GUEST, unregistred user!
表A
gysbh gysname (gysbh主键)
1 张
2 李

表B
gysbh jhdh (gysbh是表A的外键, jhdh是表B的主键,)
1 2002-01-01
2 2002-01-05
1 2002-01-02
1 2002-01-03

表C
jhdh sl dj (jhdh是表B的外键)
2002-01-01 1 2
2002-01-01 3 5
2002-01-05 1 10
2002-01-02 1 10
2002-01-03 3 10

表D
gysbh ytmoney THDH (gysb的外键,THDH是主键)
1 30 001
2 40 002

最终结果表E是这样的.

gysbh gysname Cmoney
1 张 57-表D.ytmoney(30)
2 李 10-40


Cmoney/这个字段是一个 as Cmoney的字段 
select B.JHDH,B.gysbh,Sum(C.money) AS Cmoney,A.gysname from jhdb B,
(select jhdh,sum(dj*sl) as money from jhmxb group by jhdh) C,
where B.jhdh=C.jhdh
GROUP BY B.GYSBH
它的值是表 Sum(C.money) as Cmoney-D.ytmoney 来的.

这条语句我写的有问题,
 
没看明白。

表建的不太合理。太乱了,看的头晕。
 
select gysb.gysbh,gysb.gysname,t1.cmoney-t2.ytmoney as cmoney
from (
select jhdb.jhdh,jhmxb.jhdh,jhdb.gysbh,sum(jhmxb.sl*jhmxb.dj) as cmoney
from jhdb,jhmxb
where jhdb.jhdh=jhmxb.jhdh
)as t1,gysb,
(select gysbh,sum(ytmoney) as ytmoney
from thdb
) as t2
where gysb.gysbh=t1.gysbh and gysb.gysbh=t2.gysbh

//出错

Server: Msg 8156, Level 16, State 1, Line 1
The column 'jhdh' was specified multiple times for 't1'
 
顶部