條件我有一個SQL
查詢如下如何使用動態,如果在SQL
select TransactionMode as [Modes],
TransactionCode as [Codes],
TransactionAmount as [Amount],SubTotal,Total
from TransactionDetails where CurrentTime> CAST(GETDATE()as date)
它給O/P如下
Modes Codes Amount SubTotal Total
CARDS ICICI 12 13.18 13.18
CARDS ICICI 200 219.7 219.7
CARDS ICICI 500 549.25 549.25
BUY COD 7000 42.898 38.67
CARDS SBI 400 439.4 439.4
我想計算TransactionMode
列的基礎上,委託我試過我的查詢如下
Declare @TransactionMode varchar(250);
set @TransactionMode='select TransactionMode from TransactionDetails'
IF @TransactionMode = 'CARDS'
select transactioncode as [Type], count(TransactionCode) as [No of Trans], SUM(Total) as [Amount],
ABS(SUM(Subtotal-TransactionAmount)) as [Comission] from dbo.TransactionDetails where CurrentTime> CAST(GETDATE()as date) and
Status='Active' group by transactioncode
ELSE
select transactioncode as [Type], count(TransactionCode) as [No of Trans], SUM(Total) as [Amount],
ABS(SUM(Subtotal-Total)) as [Comission] from dbo.TransactionDetails
where CurrentTime> CAST(GETDATE()as date)
and Status='Active' group by transactioncode
我期待o/p爲
Type No Of Trans Amount Comission
COD 1 38.67 4.228
ICICI 3 782.13 70.13
SBI 1 439.4 39.4
相反,我得到
Type No Of Trans Amount Comission
COD 1 38.67 4.228
ICICI 3 782.13 0
SBI 1 439.4 0
我哪裏錯了?
查詢似乎按照T-sql,它是給出正確的輸出。你必須看看有些東西>如果輸出不正確,那麼你的where條件有什麼問題。這是正確的ABS(SUM(SubTotal-TransactionAmount))as [ [作業] .OR或者公式錯誤 – KumarHarsh
我嘗試過使用'ABS(SUM(SubTotal-TransactionAmount))作爲[Comission]'和SU M(Subtotal-TransactionAmount)作爲[Comission]',但得到相同的結果:( –
'設置@ TransactionMode ='從TransactionDetails''選擇TransactionMode'因爲@ TransactionMode'永遠不會等於'CARDS',你總是會執行else條件,因爲''從TransactionDetails'中選擇TransactionMode是一個字符串而不是select語句,子查詢也會返回多個值。 – Prabhat