I have a SQL query as follows
select TransactionMode as [Modes],
TransactionCode as [Codes],
TransactionAmount as [Amount],SubTotal,Total
from TransactionDetails where CurrentTime> CAST(GETDATE()as date)
It gives o/p as follows
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
I want to calculate commission on the basis of TransactionMode columns.I tried my query as follows
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
I am expecting o/p as
Type No Of Trans Amount Comission
COD 1 38.67 4.228
ICICI 3 782.13 70.13
SBI 1 439.4 39.4
Instead I am getting
Type No Of Trans Amount Comission
COD 1 38.67 4.228
ICICI 3 782.13 0
SBI 1 439.4 0
Where I am wrong?
ABS(SUM(Subtotal-TransactionAmount)) as [Comission]andSUM(Subtotal-TransactionAmount) as [Comission]but getting the same result :(set @TransactionMode='select TransactionMode from TransactionDetails'Little worried about this line as@TransactionModewill never be equals toCARDSand you will always execute else condition because'select TransactionMode from TransactionDetails'is a string not a select statement also the subquery will return more than 1 value.ABS(SUM(a-b))seem strange. This equalsABS(SUM(a) - SUM(b)), i.e. you subtract one sum from the other and then make this positive. Doesn't it matter whether the result is negative or positive???