declare @A7_RdRecord table
(
SIverCode varchar(3),
dDate varchar(10),
bState bit,
[1Value] int
)
insert into @A7_RdRecord
select '001','2005-10-11',1,100 union all
select '002','2005-10-12',1,100 union all
select '002','2005-10-12',1,200 union all
select '003','2005-10-13',1,100 union all
select '003','2005-10-13',0,50
select * from @A7_RdRecord
select *,
isnull([1In Value],0)-isnull([1O Value],0) as [1Total]
from(
select
SIverCode,
dDate,
sum(Case bState when 1 then [1Value] end) as [1In Value],
sum(Case bState when 0 then [1Value] end) as [1O Value]
from @A7_RdRecord
group by SIverCode,dDate)a
/*
SIverCode dDate bState 1Value
--------- ---------- ------ -----------
001 2005-10-11 1 100
002 2005-10-12 1 100
002 2005-10-12 1 200
003 2005-10-13 1 100
003 2005-10-13 0 50
SIverCode dDate 1In Value 1O Value 1Total
--------- ---------- ----------- ----------- -----------
001 2005-10-11 100 NULL 100
002 2005-10-12 200 NULL 200
003 2005-10-13 100 50 50
*/