select sum(UserEP) from HL_UserExpRecorde where UserID=5 and cType=1
select sum(UserEP) from HL_UserExpRecorde where UserID=5 and cType=2想得到两数之差,也就是用第一条语句的值减去第二条语句的值,
如何拼成一条SQL语句
select sum(UserEP) from HL_UserExpRecorde where UserID=5 and cType=2想得到两数之差,也就是用第一条语句的值减去第二条语句的值,
如何拼成一条SQL语句
sum(case when cType=1 then UserEP else -UserEP end)
from
HL_UserExpRecorde
where
UserID=5 and cType in(1,2)
from
(
select
(select sum(UserEP) from HL_UserExpRecorde where UserID=5 and cType=1) as num1,
(select sum(UserEP) from HL_UserExpRecorde where UserID=5 and cType=2 ) as num2 from
UserExpRecorde
) TT
sum(case when cType=1 then UserEP else -UserEP end)
from
HL_UserExpRecorde
where
UserID=5 and (cType=1 or cType=2)
declare @table table (UserID int,cType int,UserEP int)
insert into @table
select 5,1,4 union all
select 5,1,9 union all
select 5,2,5 union all
select 5,2,7select sum(UserEP)-(select sum(UserEP)
from @table where UserID=5 and cType=2 )
from @table where UserID=5 and cType=1
declare @table table (UserID int,cType int,UserEP int)
insert into @table
select 5,1,4 union all
select 5,1,9 union all
select 5,2,5 union all
select 5,2,7select sum(UserEP)-(select sum(UserEP)
from @table where UserID=5 and cType=2 ) AS '差值'
from @table where UserID=5 and cType=1 /*
差值
-----------
1
*/