一个SQL语句中的条件

yaotomo 2013-08-14 09:35:14
n_dryweight字段可能有负值,当遇到负值时都按0计算。c_sw04字段可能不空,当不空时不进行sum


select s.c_materialid 物料编码,
case
when sum(s.n_dryweight) > 0 and s.c_sw04 is null then
sum(s.n_dryweight)
else
0
end 当日库存
from AG_TP_STOCK s
left join TB_MATERIELMAIN b
on b.c_materielid = s.c_materialid
where s.c_storageid = '20802079'
and b.c_factoryid = '2080'
group by s.c_materialid


如果我把条件c_sw04 is null写到where条件里,就无法得到那些c_sw04字段为空的物料编码。我想得到所有的物料编码,如果这个物料的c_sw04字段有值,库存就为0。

UNION的方法我也试过,虽然可以实现,但是效率太低,慢的要死。。。
如果按我上面的写法,就会提示s.c_sw04 is null不是group by 表达式。。。我该怎么写才能实现这个查询啊。。。

因为这个查询还要和其他查询做链接,所以必须得到所有的物料编码才可以。
...全文
150 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaotomo 2013-08-14
  • 打赏
  • 举报
回复
引用 4 楼 gioh0022 的回复:
想到只能union
UNION的话,需要3分钟才能查出来,再和其他查询做连接 整改查询需要10分钟左右。。我想用户是受不了的
  • 打赏
  • 举报
回复
引用 3 楼 yaotomo 的回复:
[quote=引用 1 楼 gioh0022 的回复:] sum(s.c_sw04)会怎样
sw_04字段是拣配单号,是字母和数字的组合,不可能sum。。。 count倒是可以,但是用count的话,就成了一个物料下所有c_sw04字段都为空才 sum(s.n_dryweight) 我想实现的是,如果同一个物料下的一个记录的c_sw04为空,就sum上,如果不为空,就不往上加[/quote] 想到只能union
yaotomo 2013-08-14
  • 打赏
  • 举报
回复
引用 1 楼 gioh0022 的回复:
sum(s.c_sw04)会怎样
sw_04字段是拣配单号,是字母和数字的组合,不可能sum。。。 count倒是可以,但是用count的话,就成了一个物料下所有c_sw04字段都为空才 sum(s.n_dryweight) 我想实现的是,如果同一个物料下的一个记录的c_sw04为空,就sum上,如果不为空,就不往上加
baofeng0722 2013-08-14
  • 打赏
  • 举报
回复
关注中,希望能学习到
  • 打赏
  • 举报
回复
sum(s.c_sw04)会怎样
yaotomo 2013-08-14
  • 打赏
  • 举报
回复
谢谢yinan9 ,真是救星啊! 回8#朋友,不加不行的,很复杂的一个报表。。。
  • 打赏
  • 举报
回复
引用 8 楼 u011224865 的回复:

select  s.c_materialid 物料编码,
               sum (case
                 when s.n_dryweight> 0 and s.c_sw04 is null then s.n_dryweight      
                 else
                  0 )
               end 当日库存
          from AG_TP_STOCK s
where s.c_storageid = '20802079'
group by s.c_materialid
请问不加外链接不行吗?
好像是~~~同问~~~
小手很脏 2013-08-14
  • 打赏
  • 举报
回复

select  s.c_materialid 物料编码,
               sum (case
                 when s.n_dryweight> 0 and s.c_sw04 is null then s.n_dryweight      
                 else
                  0 )
               end 当日库存
          from AG_TP_STOCK s
where s.c_storageid = '20802079'
group by s.c_materialid
请问不加外链接不行吗?
yinan9 2013-08-14
  • 打赏
  • 举报
回复
不好意思,上面的应该有点问题

select  s.c_materialid 物料编码,
               sum (case
                 when s.n_dryweight> 0 and s.c_sw04 is null then s.n_dryweight      
                 else 0  end ) 当日库存
          from AG_TP_STOCK s
          left join TB_MATERIELMAIN b
            on b.c_materielid = s.c_materialid
         where s.c_storageid = '20802079'
           and b.c_factoryid = '2080'
         group by s.c_materialid
yinan9 2013-08-14
  • 打赏
  • 举报
回复
稍作修改即可

select  s.c_materialid 物料编码,
               sum (case
                 when s.n_dryweight> 0 and s.c_sw04 is null then s.n_dryweight      
                 else
                  0 )
               end 当日库存
          from AG_TP_STOCK s
          left join TB_MATERIELMAIN b
            on b.c_materielid = s.c_materialid
         where s.c_storageid = '20802079'
           and b.c_factoryid = '2080'
         group by s.c_materialid

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧