分组依据 - 使用绝对值

时间:2018-04-16 15:58:45

标签: tsql group-by sql-server-2014 running-total

我正在尝试显示会计报告,其中显示总交易,空白,交易费用以及每种交易类型的总金额。

<TextField fx:id="userName" layoutX="15.0" layoutY="14.0" onAction="#getUserName" onMouseReleased="#getUserName" promptText="User Name" />
<Label layoutX="15.0" layoutY="42.0" text="User Name" />
<TextField fx:id="userPwd" layoutX="15.0" layoutY="72.0" nAction="#pwdController" promptText="Password" />
<Label layoutX="15.0" layoutY="100.0" text="Password" />
<Label fx:id="warningLabel" focusTraversable="false" layoutX="15.0" layoutY="138.0" prefHeight="18.0" prefWidth="170.0" text="securitywarning" visible="false" />
<Separator layoutY="168.0" prefHeight="7.0" prefWidth="200.0" />

但我要显示的内容如下所示:

TransactionType  Amount  TransactionCount   TotalAmount
AgentCredit      -$1.00   49                 -$49.00
MailFee          -$1.25   11                 -$13.75
MailFee           $1.25   531                 $663.75
HardCardFee      -$5.00   7                 -$35.00
HardCardFee       $5.00   239                $1,195.00
QuotaHuntFee     -$2.00   1                 -$2.00
QuotaHuntFee      $2.00   202                $404.00

是否可以使用金额的绝对值对交易类型进行分组,并计算总金额以及交易计数&amp;虚假计数?

这是在SQL Server 2014上。

谢谢,

2 个答案:

答案 0 :(得分:1)

对于您提供的数据,您的结果会有一些混乱。 HardCardFee在您提供的示例中有7个和23个,但是您想要返回232总数?.. MailFee也有一些不一致的数学。此外,您的'空洞'在第一行返回0;然而,似乎有49个?

也许这个查询可以让你开始走正确的道路:

DECLARE @Table TABLE (TransactionType varchar(20), Amount decimal(10,2), TransactionCount int, TotalAmount decimal(10,2))
INSERT @Table
VALUES  ('AgentCredit'  ,-$1.00  ,49   ,-$49.00  ),
        ('MailFee'      ,-$1.25  ,11   ,-$13.75  ),
        ('MailFee'      ,$1.25   ,531  ,$663.75  ),
        ('HardCardFee'  ,-$5.00  ,7    ,-$35.00  ),
        ('HardCardFee'  ,$5.00   ,23   ,$1195.00 ),
        ('QuotaHuntFee' ,-$2.00  ,1    ,-$2.00   ),
        ('QuotaHuntFee' ,$2.00   ,202  ,$404.00  )

;WITH c AS (
SELECT TransactionType, Amount, TransactionCount, TotalAmount,
CASE WHEN t.Amount + ABS(t.Amount) = 0 THEN '-' ELSE '' END + 
CAST(t.TransactionCount AS VARCHAR(10)) AS TCount
FROM @Table t
)

SELECT t.TransactionType
      ,MAX(t.Amount) AS Amount
      ,SUM(CAST(t.TCount AS INT)) AS TransactionCount
      ,SUM(t.TotalAmount) AS TotalAmount
      ,SUM(ABS(t.TransactionCount)) AS TotalTrans
      ,ABS(MIN(t.TCount)) AS Voids
FROM c t
GROUP BY TransactionType

同样,不确定提供的某些值。

答案 1 :(得分:1)

我认为这样做

declare @T table (nm varchar(20), prc smallmoney, amt int);
insert into @T values  
       ('AgentCredit', -1.00, 49)  
     , ('MailFee', -1.25, 11)
     , ('MailFee', 1.25, 531)
     , ('HardCardFee', -5.00, 7)
     , ('HardCardFee', 5.00, 239)
     , ('QuotaHuntFee', -2.00, 1)
     , ('QuotaHuntFee', 2.00, 202);
with cte as 
(
select t.*, (t.prc * t.amt) as net 
     , count(*) over (partition by t.nm, abs(t.prc)) as cnt
     , row_number() over (partition by t.nm, abs(t.prc) order by t.prc) as rn
     , lag(t.prc) over (partition by t.nm, abs(t.prc) order by t.prc) as prPrc  
     , lag(t.amt) over (partition by t.nm, abs(t.prc) order by t.prc) as prAmt
     , case when lag(t.prc) over (partition by t.nm, abs(t.prc) order by t.prc) <  0 then t.amt - lag(t.amt) over (partition by t.nm, abs(t.prc) order by t.prc) 
            else t.amt 
       end as bal
from @T t 
)
select *, ISNULL(t.prAmt, 0) as void
     , bal*prc as nnet
from cte t 
where t.cnt = 1 
   or t.rn = 2
order by t.nm, t.prc;