クエリ結果の最後に説明列を追加したいと考えています。たとえば、私は
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