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

简单问题,求进出差异数
类别           状态
A                   0
B                   1
C                   0
D                   0
A                   1
A                   0
E                   0
B                   0
/*0代表售出,1代表退货,求总出货数,售出-退货*/

------解决方案--------------------
create table tb(类别 varchar(10),状态 int)
insert into tb values( 'A ', 0)
insert into tb values( 'B ', 1)
insert into tb values( 'C ', 0)
insert into tb values( 'D ', 0)
insert into tb values( 'A ', 1)
insert into tb values( 'A ', 0)
insert into tb values( 'E ', 0)
insert into tb values( 'B ', 0)
select 总出货数 = (select count(*) from tb where 状态 = 0) - (select count(*) from tb where 状态 = 1)

drop table tb

/*
总出货数
-----------
4

(所影响的行数为 1 行)

*/


------解决方案--------------------
select sum(case when 状态=0 then 1 else -1 end) from [Table]