求高人完善SQL server数据库语句
select a.zy_id AS zyblh, b.yaop_id AS xmbh, c.mc AS xmmc, NULL AS cd, NULL AS gg, CASE WHEN c.zylb = 03 THEN 1
WHEN c.zylb = 02 THEN 2
WHEN c.zylb = 01 THEN 3
WHEN c.zylb = 06 THEN 4
ELSE 0 END AS lx,
c.dw AS dwmc,
'1' AS js,
b.dj,
b.sl,
b.je AS zj,
d.mc AS hjr,
a.rq AS hjsj,
a.zfbz
from h_yaj a,h_sfdmx b,h_yaop c,h_yis d
where a.djh = b.sfd_id
and b.yaop_id = c.id
and a.sfy = d.id
and a.zfbz = '0'
and a.zy_id = '00006951'
想在这个SQL明细汇总表的基础上加入退费的明细汇总,需要加入h_ckdmx.e表,。
a表与b表,和a表与e表的关系为: 如果没有退费时就为a.djh = b.sfd_id ;如有退费发生时,a.djh 同时包含b.sfd_id 和e.ckd_id 。
b表与e表的关系为:b.id = e.sfdmx_id 。
并且 如果有退费发生时sl (数量) 需要 = (b.sl 减去 e表里b.id = e.sfdmx_id 项目的sl) ; zj(总价)需要 = (b.je 减去e表里b.id = e.sfdmx_id 项目的je)
求老师完善!