【问题标题】:I want to make two Queries into one by a variable我想通过一个变量将两个查询合二为一
【发布时间】:2018-09-03 11:18:35
【问题描述】:

我想通过一个变量将两个查询合二为一

ALTER PROCEDURE [dbo].[Balance]
     @userId nvarchar(200)
AS
BEGIN

     DECLARE @parchesQueryAdd decimal(18,2),@parchesQueryRemove decimal(18,2), @topupQuery decimal(18,2), @Balance decimal(18,2), @totalamount decimal(18,2)
/****** this two Querys starts ******/

     SET @parchesQueryAdd = (SELECT SUM(Amount * CurrentBalanceCurrency) from UserBalance where BalanceForId = @userId and AmountType = 10)
     SET @parchesQueryRemove = (SELECT SUM(Amount * CurrentBalanceCurrency) from UserBalance where BalanceForId = @userId and AmountType = 20)
/****** End ******/

     SET @Balance = @parchesQueryAdd - @parchesQueryRemove

     SET @topupQuery = (SELECT SUM(Amount * Quentity) from TopUpRecords where TopupById = @userId)

     SET @totalamount= @Balance - @topupQuery

     PRINT @totalamount
END

【问题讨论】:

  • SUM(CASE WHEN...)

标签: sql sql-server stored-procedures sql-server-2016


【解决方案1】:

您可以只对总和或总计使用单个查询

   select sum( case when AmountType = 10 
                    then Amount * CurrentBalanceCurrency else 0 end ) parchesQueryAdd
       ,  sum( case when AmountType = 20 
                    then Amount * CurrentBalanceCurrency else 0 end ) parchesQueryRemove
       ,  sum( case when AmountType = 10 
                    then Amount * CurrentBalanceCurrency else 0 end )  -
          sum( case when AmountType = 20 
                    then Amount * CurrentBalanceCurrency else 0 end ) totQuery 
   from UserBalance where BalanceForId = @userId 

【讨论】:

    【解决方案2】:

    您可以使用条件聚合函数来设置@Balance而不是两个查询。

    DECLARE 
         @parchesQueryAdd decimal(18,2),
         @parchesQueryRemove decimal(18,2), 
         @topupQuery decimal(18,2), 
         @Balance decimal(18,2), 
         @totalamount decimal(18,2)
    
    
    SELECT @Balance = SUM(CASE WHEN  AmountType = 10 THEN Amount * CurrentBalanceCurrency ELSE 0 END)
                      - SUM(CASE WHEN  AmountType = 20 THEN Amount * CurrentBalanceCurrency ELSE 0 END)
    FROM UserBalance
    WHERE BalanceForId = @userId 
    GROUP BY BalanceForId
    
    
     SET @topupQuery = (SELECT SUM(Amount * Quentity) from TopUpRecords where TopupById = @userId)
    
    SET @totalamount= @Balance - @topupQuery
    
    PRINT @totalamount
    

    【讨论】:

      【解决方案3】:

      据我了解

      create table #oneVariable (type int, amt int, bal int)
      
      insert #oneVariable values(10,10,20),(10,10,20),(20,10,20),(30,10,20)
      
      select type, sum(amt*bal) sumOfAmt_Bal from #oneVariable group by type  
      -- type 10 has amt*bal 400. And, type 20 has 200.
      -- So according by you formula, Balance will be 200.
      -- Whereas, type 30 has been eliminated.
      
      declare @balance int
      SET @Balance = (
       select sum(
         case type 
           when 10 then amt * bal
           when 20 then -(amt * bal)
         end
       ) sumOfAmt_Bal 
       from #oneVariable
      )
      print @balance
      

      【讨论】:

        猜你喜欢
        • 2021-06-25
        • 2019-06-18
        • 1970-01-01
        • 2014-06-23
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多