日期:2014-05-18  浏览次数:20408 次

初哥统计问题
单号               类别           状态
1234               AA                 0
2234               BB                 1          
2364               AA                 0
1234               CC                 0
1234               DD                 0
1256               EE                 1
2556               FF                 0
2556               GG                 0
1147               22                 0
----------------------------
一行等于一件,0代表卖出,1代表退货
结果
销售单数(统计所有单数,有一些单里面有多个商品)           销售数量(统计卖出数量=卖出-退货,类别22是增品,不在统计范围,但计入销售单数)

给果为销售单数与销售数量两列.

------解决方案--------------------
select 单号,类别,sum(case when 状态=0 then 1 else 0 end) 销售数量 from table group by 单号,类别
------解决方案--------------------
select a.a,b.b from (select count(distinct(单号)) as a from #a where 类别 <> '22 ' ) a,(select (select count(*) from #a where 状态= '0 ')-(select count(*) from #a where 状态= '1 ' ) as b) b
------解决方案--------------------
select 销售单数 = (select count(*) from tb) , 销售数量 = (select sum(case when 状态 = 0 then 1 else -1 end) from tb where 类别 <> '22 ')
------解决方案--------------------
select 销售单数=count(*),销售数量=sum(case when 类别= '22 ' then 0
when 状态=0 then 1 else -1 end)
from 表