日期:2014-05-17  浏览次数:20531 次

sql 多条件查询
SQL code

  select SUM(OrderNum) as OrderNum,Sum(ItemNoNum),electroorderno from (select distinct OrderNum,ItemNoNum,ItemNo,ElectroOrderNo from FA_WMSDB.ServicePos.dbo.SnService where customer_id='00001') t group by ElectroOrderNo,OrderNum



一:
子句语句:
SQL code
select distinct OrderNum,ItemNoNum,ItemNo,ElectroOrderNo from FA_WMSDB.ServicePos.dbo.SnService where customer_id='00001'

结果:

注意订单号!!!

全部查询结果:


二:
子句语句:
SQL code
select distinct OrderNum,ItemNoNum,ItemNo,ElectroOrderNo from FA_WMSDB.ServicePos.dbo.SnService where customer_id='00002'

结果:

注意订单号!!!

全部查询结果:




第一种结果是错误的。总数应该是395,不该再sum了。

问题:
我改对了一种,另一种就错了。如何才能两者兼容



------解决方案--------------------
try this,
SQL code

 select sum(distinct OrderNum) as OrderNum,
        sum(ItemNoNum),
        electroorderno
 from (select distinct
              OrderNum ,
              ItemNoNum ,
              ItemNo ,
              ElectroOrderNo
        from FA_WMSDB.ServicePos.dbo.SnService
        where customer_id = '00001') t
 group by ElectroOrderNo, OrderNum

------解决方案--------------------
不同客户的字段意义不同,除非你在客户表增加分类字段,然后分类处理,否则没有办法