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

时间:2018-09-03 11:18:35

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

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

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

3 个答案:

答案 0 :(得分:1)

您可以使用条件聚合函数来设置plugins: [ [ "transform-imports", { vuetify: { transform: "vuetify/es5/components/${member}", /* change the preventFullImport property to false */ preventFullImport: true } } ] ] 而不是两个查询。

@Balance

答案 1 :(得分: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 :(得分:0)

据我了解

print(len(flaggedrows))