在查询结果上添加描述性/评论列

时间:2013-11-12 18:08:06

标签: sql sql-server sql-server-2008

我想在查询结果的末尾添加一个描述性列。例如 我有

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 

4 个答案:

答案 0 :(得分:2)

您可以将CASE部分合并到周围的查询中。 E.g:

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;

编辑:根据@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;

答案 1 :(得分: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;

答案 2 :(得分:1)

当我需要操作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 

答案 3 :(得分:0)

使用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 ....