I want to add a descriptive column at the end of my query results. For example I have
我想在查询结果的末尾添加一个描述性列。比如我有
SELECT sum(amount) as Balance FROM tbDebits WHERE CustomerID =@CustomerID;
Now if the Balance is positive I want to add another column called 'Description' in my query results describing each result as positive or negative. Any ideas?
现在,如果余额是正数,我想在我的查询结果中添加另一个名为“描述”的列,将每个结果描述为正面或负面。有任何想法吗?
Here is my original query:
这是我的原始查询:
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
4 个解决方案
#1
1
I like to use OUTER APPLY when I need to manipulate the results of a SELECT. I hope it is useful.
当我需要操作SELECT的结果时,我喜欢使用OUTER APPLY。我希望它有用。
SELECT t.CustomerID,
c.name,
c.Surname,
Balance.Custom,
case
when sum(Balance.Custom)>0 then 'positive'
when sum(Balance.Custom)<0 then 'negative'
else 'zero'
end as [description]
FROM [Transaction] AS t
INNER JOIN Customer AS c
ON t.CustomerID = c.CustomerID
OUTER APPLY (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 Custom) AS Balance
GROUP BY t.CustomerID,
c.name,
c.Surname
#2
2
SELECT
sum(amount) as balance,
case
when sum(amount)>0 then 'positive'
when sum(amount)<0 then 'negative'
else 'zero'
end as description
FROM tbDebits
WHERE CustomerID=@CustomerID;
#3
2
You could incorporate the CASE
part in a surrounding query. E.g.:
您可以将CASE部分合并到周围的查询中。例如。:
SELECT balance, CASE WHEN balance > 0 THEN 'positive!'
WHEN balance < 0 THEN 'negative!'
ELSE 'exactly zero'
END
FROM (SELECT SUM(amount) AS balance
FROM tbDebits
WHERE CustomerID = @CustomerID) t;
EDIT: Adding the customer's details, as per the comment by @nectarines:
编辑:根据@nectarines的评论添加客户的详细信息:
SELECT name, surname, balance,
CASE WHEN balance > 0 THEN 'positive!'
WHEN balance < 0 THEN 'negative!'
ELSE 'exactly zero'
END
FROM (SELECT customerid, name, surname,
SUM(amount) AS balance
FROM tbDebits
WHERE CustomerID = @CustomerID
GROUP BY customerid, name, surname -- note: there's only one name/surname per customerid
) t;
#4
0
Use WITH and CASE:
使用WITH和CASE:
WITH A as (
... complex query goes here, do not include ORDER BY...
)
SELECT A.*,
CASE WHEN A.Balance > 0 then 'pos'
CASE WHEN A.Balance < 0 then 'neg' else 'zero' end as Description
FROM A
ORDER BY ....
#1
1
I like to use OUTER APPLY when I need to manipulate the results of a SELECT. I hope it is useful.
当我需要操作SELECT的结果时,我喜欢使用OUTER APPLY。我希望它有用。
SELECT t.CustomerID,
c.name,
c.Surname,
Balance.Custom,
case
when sum(Balance.Custom)>0 then 'positive'
when sum(Balance.Custom)<0 then 'negative'
else 'zero'
end as [description]
FROM [Transaction] AS t
INNER JOIN Customer AS c
ON t.CustomerID = c.CustomerID
OUTER APPLY (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 Custom) AS Balance
GROUP BY t.CustomerID,
c.name,
c.Surname
#2
2
SELECT
sum(amount) as balance,
case
when sum(amount)>0 then 'positive'
when sum(amount)<0 then 'negative'
else 'zero'
end as description
FROM tbDebits
WHERE CustomerID=@CustomerID;
#3
2
You could incorporate the CASE
part in a surrounding query. E.g.:
您可以将CASE部分合并到周围的查询中。例如。:
SELECT balance, CASE WHEN balance > 0 THEN 'positive!'
WHEN balance < 0 THEN 'negative!'
ELSE 'exactly zero'
END
FROM (SELECT SUM(amount) AS balance
FROM tbDebits
WHERE CustomerID = @CustomerID) t;
EDIT: Adding the customer's details, as per the comment by @nectarines:
编辑:根据@nectarines的评论添加客户的详细信息:
SELECT name, surname, balance,
CASE WHEN balance > 0 THEN 'positive!'
WHEN balance < 0 THEN 'negative!'
ELSE 'exactly zero'
END
FROM (SELECT customerid, name, surname,
SUM(amount) AS balance
FROM tbDebits
WHERE CustomerID = @CustomerID
GROUP BY customerid, name, surname -- note: there's only one name/surname per customerid
) t;
#4
0
Use WITH and CASE:
使用WITH和CASE:
WITH A as (
... complex query goes here, do not include ORDER BY...
)
SELECT A.*,
CASE WHEN A.Balance > 0 then 'pos'
CASE WHEN A.Balance < 0 then 'neg' else 'zero' end as Description
FROM A
ORDER BY ....