我想在查询结果的末尾添加一个描述性列。例如我有
SELECT sum(amount) as Balance FROM tbDebits WHERE CustomerID =@CustomerID;
现在,如果余额是正数,我想在查询结果中添加另一列名为“描述”的列,将每个结果描述为正数或负数。有任何想法吗?
这是我的原始查询:
SELECT t.CustomerID, c.name, c.Surname, (SELECT (
(SELECT ISNULL(SUM(cashout),0)-
((select ISNULL(sum(Buyin),0) from [Transaction] where TYPE='Credit' and CustomerID=t.CustomerID )
+ (select ISNULL(sum(Paid),0) from [Transaction] where TYPE='Credit' and CustomerID=t.CustomerID ))
FROM [transaction]
WHERE TYPE='Credit'
AND CustomerID=t.CustomerID
)
-------------------
+
(
(SELECT ISNULL(SUM(cashout),0)
- (select ISNULL(sum(Paid),0) from [Transaction] where TYPE='Debit' AND Cashout>buyin and CustomerID=t.CustomerID )
+ (select ISNULL(sum(Cashout),0)- (select ISNULL(sum(PAID),0) from [Transaction] where TYPE='Debit' AND Cashout<buyin and CustomerID=t.CustomerID )
from [Transaction] where TYPE='Debit' AND Cashout<Buyin and CustomerID=t.CustomerID )
+ (select ISNULL(sum(Cashout),0)- (select ISNULL(sum(PAID),0) from [Transaction] where TYPE='Debit' AND Cashout=buyin and CustomerID=t.CustomerID )
from [Transaction] where TYPE='Debit' AND Cashout=Buyin and CustomerID=t.CustomerID )
FROM [Transaction]
WHERE CustomerID=t.CustomerID
AND TYPE='Debit'
AND Cashout>buyin )
)
--------------
-
(
select ISNULL(sum(Paid),0)
from [Transaction]
where type='Debit Settlement'
AND CustomerID =t.CustomerID
)
--------------
+
(
select ISNULL(sum(Paid),0)
from [Transaction]
where type='Credit Settlement'
AND CustomerID =t.CustomerID
)
)) as Balance FROM [Transaction] as t
inner JOIN Customer AS c
on t.CustomerID = c.CustomerID
GROUP BY t.CustomerID, c.name, c.Surname