一个SQL语句中的条件
本帖最后由 yaotomo 于 2013-08-14 09:35:57 编辑 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
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
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