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 に答える
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 に答える