| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 632 人关注过本帖
标题:求高人完善SQL server数据库语句
只看楼主 加入收藏
jinyizgw
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求高人完善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)

求老师完善!
搜索更多相关主题的帖子: 数据库 server where 
2012-07-28 13:41
jinyizgw
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-28
收藏
得分:0 
大神门啊,别这么吝啬好吧,帮帮忙啊
2012-07-30 20:17
netlin
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:24
帖 子:544
专家分:4308
注 册:2012-4-9
收藏
得分:20 
楼主,大概看懂了你的要求,你的程序也写得很清楚。
按你的要求,对你的程序做了一些修改,主要是提供一个思路吧,你可能要在你的数据环境中调试一下才能运行。

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-isnull(e.sl,0) as sl,
           b.je-isnull(e.je,0) AS zj,
           d.mc AS hjr,
           a.rq AS hjsj,
           a.zfbz
from h_yaj a
    inner join h_sfdmx b on a.djh = b.sfd_id
    inner join h_yaop c on b.yaop_id = c.id
    inner join h_yis d on a.sfy = d.id
    left join h_ckdmx e on b.id = e.sfdmx_id
where a.zfbz = '0'
    and a.zy_id = '00006951'


做自己喜欢的事!
2012-08-04 18:01
快速回复:求高人完善SQL server数据库语句
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016043 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved