【发布时间】: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