拼一条SQL语句

maomao90 2010-01-28 01:11:39
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语句
...全文
68 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2010-01-28
  • 打赏
  • 举报
回复

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,7

select sum(UserEP)-(select sum(UserEP)
from @table where UserID=5 and cType=2 ) AS '差值'
from @table where UserID=5 and cType=1

/*
差值
-----------
1
*/
budong0000 2010-01-28
  • 打赏
  • 举报
回复
眼神不好不好意思
叶子 2010-01-28
  • 打赏
  • 举报
回复

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,7

select sum(UserEP)-(select sum(UserEP)
from @table where UserID=5 and cType=2 )
from @table where UserID=5 and cType=1
--小F-- 2010-01-28
  • 打赏
  • 举报
回复
select 
sum(case when cType=1 then UserEP else -UserEP end)
from
HL_UserExpRecorde
where
UserID=5 and (cType=1 or cType=2)
budong0000 2010-01-28
  • 打赏
  • 举报
回复
没看懂呢,你的两个value不一样么?
ws_hgo 2010-01-28
  • 打赏
  • 举报
回复
select num1-num2
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
ACMAIN_CHM 2010-01-28
  • 打赏
  • 举报
回复
select (select sum(UserEP) from HL_UserExpRecorde where UserID=5 and cType=1)-(select sum(UserEP) from HL_UserExpRecorde where UserID=5 and cType=2)
百年树人 2010-01-28
  • 打赏
  • 举报
回复
select 
sum(case when cType=1 then UserEP else -UserEP end)
from
HL_UserExpRecorde
where
UserID=5 and cType in(1,2)

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧