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语句

解决方案 »

  1.   

    select 
      sum(case when cType=1 then UserEP else -UserEP end)
    from
      HL_UserExpRecorde
    where
      UserID=5 and cType in(1,2) 
      

  2.   

    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)
      

  3.   

    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
      

  4.   

    没看懂呢,你的两个value不一样么?
      

  5.   

    select 
      sum(case when cType=1 then UserEP else -UserEP end)
    from
      HL_UserExpRecorde
    where
      UserID=5 and (cType=1 or cType=2)
      

  6.   


    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 
      

  7.   


    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
    */