0

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

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

4 回答 4

2

您可以将该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;

编辑:根据@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;
于 2013-11-16T18:30:03.003 回答
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;
于 2013-11-16T18:33:43.973 回答
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 
于 2013-11-16T21:59:20.017 回答
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 ....
于 2013-11-16T19:41:51.580 回答