分组依据 - 使用绝对值
Group By - Using Absolute Values
我正在尝试显示一份会计报告,其中显示了每种交易类型的总交易、无效、交易费用和总金额。
TransactionType Amount TransactionCount TotalAmount
AgentCredit -.00 49 -.00
MailFee -.25 11 -.75
MailFee .25 531 3.75
HardCardFee -.00 7 -.00
HardCardFee .00 239 ,195.00
QuotaHuntFee -.00 1 -.00
QuotaHuntFee .00 202 4.00
但我想要显示的内容如下所示:
TransactionType Amount TransactionCount TotalAmount TotalTrans Voids
AgentCredit -.00 49 -.00 49 0
MailFee .25 520 0.00 531 11
HardCardFee .00 232 ,160.00 239 7
QuotaHuntFee .00 201 2.00 202 1
是否可以使用金额的绝对值对交易类型进行分组,并计算总计以及交易次数和作废次数?
这是在 SQL Server 2014 上。
谢谢,
您的结果与您提供的数据有些混淆。 HardCardFee 在您提供的示例中有 7 和 23,但您想要 return 232 的总数?.. MailFee 也有一些不一致的数学。此外,第一行的 'Voids' returns 0;不过好像有49个?
也许这个查询可以让您走上正确的道路:
DECLARE @Table TABLE (TransactionType varchar(20), Amount decimal(10,2), TransactionCount int, TotalAmount decimal(10,2))
INSERT @Table
VALUES ('AgentCredit' ,-.00 ,49 ,-.00 ),
('MailFee' ,-.25 ,11 ,-.75 ),
('MailFee' ,.25 ,531 ,3.75 ),
('HardCardFee' ,-.00 ,7 ,-.00 ),
('HardCardFee' ,.00 ,23 ,95.00 ),
('QuotaHuntFee' ,-.00 ,1 ,-.00 ),
('QuotaHuntFee' ,.00 ,202 ,4.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;
我正在尝试显示一份会计报告,其中显示了每种交易类型的总交易、无效、交易费用和总金额。
TransactionType Amount TransactionCount TotalAmount
AgentCredit -.00 49 -.00
MailFee -.25 11 -.75
MailFee .25 531 3.75
HardCardFee -.00 7 -.00
HardCardFee .00 239 ,195.00
QuotaHuntFee -.00 1 -.00
QuotaHuntFee .00 202 4.00
但我想要显示的内容如下所示:
TransactionType Amount TransactionCount TotalAmount TotalTrans Voids
AgentCredit -.00 49 -.00 49 0
MailFee .25 520 0.00 531 11
HardCardFee .00 232 ,160.00 239 7
QuotaHuntFee .00 201 2.00 202 1
是否可以使用金额的绝对值对交易类型进行分组,并计算总计以及交易次数和作废次数?
这是在 SQL Server 2014 上。
谢谢,
您的结果与您提供的数据有些混淆。 HardCardFee 在您提供的示例中有 7 和 23,但您想要 return 232 的总数?.. MailFee 也有一些不一致的数学。此外,第一行的 'Voids' returns 0;不过好像有49个?
也许这个查询可以让您走上正确的道路:
DECLARE @Table TABLE (TransactionType varchar(20), Amount decimal(10,2), TransactionCount int, TotalAmount decimal(10,2))
INSERT @Table
VALUES ('AgentCredit' ,-.00 ,49 ,-.00 ),
('MailFee' ,-.25 ,11 ,-.75 ),
('MailFee' ,.25 ,531 ,3.75 ),
('HardCardFee' ,-.00 ,7 ,-.00 ),
('HardCardFee' ,.00 ,23 ,95.00 ),
('QuotaHuntFee' ,-.00 ,1 ,-.00 ),
('QuotaHuntFee' ,.00 ,202 ,4.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;