Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tsql 按组-使用绝对值_Tsql_Group By_Sql Server 2014_Cumulative Sum - Fatal编程技术网

Tsql 按组-使用绝对值

Tsql 按组-使用绝对值,tsql,group-by,sql-server-2014,cumulative-sum,Tsql,Group By,Sql Server 2014,Cumulative Sum,我试图显示一个会计报告,其中显示了交易总额、作废、交易费用以及每种交易类型的总金额 TransactionType Amount TransactionCount TotalAmount AgentCredit -$1.00 49 -$49.00 MailFee -$1.25 11 -$13.75 MailFee $1.25 531

我试图显示一个会计报告,其中显示了交易总额、作废、交易费用以及每种交易类型的总金额

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
但我想显示的内容如下所示:

TransactionType Amount  TransactionCount TotalAmount    TotalTrans Voids
AgentCredit    -$1.00    49               -$49.00         49         0
MailFee         $1.25    520               $650.00        531        11
HardCardFee     $5.00    232               $1,160.00      239        7
QuotaHuntFee    $2.00    201               $402.00        202        1
是否可以使用金额的绝对值对交易类型进行分组,并计算总金额以及交易计数和无效计数

这是在SQL Server 2014上


谢谢,

您提供的数据与您的结果有点混淆。HardCardFee在您提供的示例中有7和23,但您希望返回232作为总数?。。MailFee也有一些不一致的数学。此外,您的“Voids”第一行返回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

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

您的结果与您提供的数据有点混淆。HardCardFee在您提供的示例中有7和23,但您希望返回232作为总数?。。MailFee也有一些不一致的数学。此外,您的“Voids”第一行返回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
同样,我也不确定提供的一些值。

我想这就行了

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;
我想这样就行了

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;

使用条件聚合:transactiontype,金额<0时的sumcase,然后TransactionCount else 0结束,Countcase…来自。。。按Transactiontype分组…使用条件聚合:Transactiontype,金额<0时的sumcase,然后TransactionCount else 0结束,Countcase…来自。。。按事务类型分组…谢谢BJones-格式化时,我似乎删除了应该存在的两个数字。如果报告了-49,则没有无效感谢BJones-格式化时,我似乎删除了应该存在的两个数字。如果报告了-49,则没有无效我知道您在这里做了什么,但它仍然让我感到震惊。谢谢。我知道你在这里做了什么,但这仍然让我感到震惊。非常感谢。