我有一个具有重复值的表。我想做的是汇总各列。我唯一的问题是我不想基于表中的列NewTransaction
对所有要重复的重复值求和。
RecoveryKey DateTime Duration NewTransaction Variable8 CallTypeID
--------------------------------------------------------------------------
7994113912466 12/4/18 16:26 19 Y 152643-5657 -1
7994113912470 12/4/18 16:26 1168 Y 152643-5657 6390
7994113912751 12/4/18 16:51 2686 N 152643-5657 6390
所以从表中可以看到,我要为Max(DateTime)
选择Variable8
的记录是具有NewTransaction == Y
的重复记录。
与具有NewTransaction == N
如果您这样做,得到的值将低于-
RecoveryKey DateTime Duration NewTransaction Variable8 CallTypeID
--------------------------------------------------------------------------
7994113912470 12/4/18 16:26 1168 Y 152643-5657 6390
7994113912751 12/4/18 16:51 2686 N 152643-5657 6390
为此,我需要的是max(RecoveryKey)
,max(DateTime)
,Sum(Duration)
。我的最终结果将如下所示
RecoveryKey DateTime Duration NewTransaction Variable8 CallTypeID
---------------------------------------------------------------------------
7994113912470 12/4/18 16:26 3854 Y 152643-5657 6390
答案 0 :(得分:0)
我想这就是你想要的
select max(RecoveryKey)
, distinct DateTime
, Sum(Duration)
, NewTransaction, Variable8
, CallTypeID
from MyTable
group by NewTransaction, Variable8
having datetime = max(datetime);
答案 1 :(得分:0)
基于这个问题,我相信您正在尝试做以下两件事:
max([DateTime])
和Variable8
组合,向下过滤一条记录,即NewTransaction
。NewTransaction
值为Y
的记录中包含的数据,而不显示通过步骤1的筛选器得到的记录中的总Duration
。答案
with initial_filter as
(
select *
, row_number() over (partition by Variable8, NewTransaction order by [DateTime] desc, RecoveryKey desc) as rnk_a
, row_number() over (partition by Variable8 order by NewTransaction desc, [DateTime] desc, RecoveryKey desc) as rnk_b
from MyTable as t
)
select max(b.RecoveryKey) as RecoveryKey
, max(b.[DateTime]) as [DateTime]
, sum(a.Duration) as Duration
, max(b.NewTransaction) as NewTransaction
, max(b.Variable8) as Variable8
, max(b.CallTypeID) as CallTypeID
from initial_filter as a
left join initial_filter as b on a.RecoveryKey = b.RecoveryKey and b.rnk_b = 1
where a.rnk_a = 1
结果:
+---------------+-------------------------+----------+----------------+-------------+------------+
| RecoveryKey | DateTime | Duration | NewTransaction | Variable8 | CallTypeID |
+---------------+-------------------------+----------+----------------+-------------+------------+
| 7994113912470 | 2018-12-04 16:26:00.000 | 3854 | Y | 152643-5657 | 6390 |
+---------------+-------------------------+----------+----------------+-------------+------------+
答案 2 :(得分:0)
您好Abdulquadir Shaikh,
也许这就是您想要的。
create table test22
(
RecoveryKey varchar(64),
[DateTime] datetime,
Duration int,
NewTransaction varchar(30),
Variable8 varchar(64),
CallTypeID int
)
insert into test22 values
('7994113912466','12/4/18 16:26:00',19,'Y','152643-5657',-1),
('7994113912470','12/4/18 16:26:00',1168,'Y','152643-5657',6390),
('7994113912751','12/4/18 16:51:00',2686,'N','152643-5657',6390)
;WITH CTE AS
(
select
max(RecoveryKey) as RecoveryKey,
max([DateTime]) as [DateTime],
max(Duration) as Duration,
NewTransaction,
Variable8,
max(CallTypeID) as CallTypeID
from test22
group by Variable8,NewTransaction
)
SELECT
MAX(RecoveryKey) AS RecoveryKey,
MAX([DateTime]) AS [DateTime],
SUM(Duration) AS Duration,
MAX(NewTransaction) AS NewTransaction,
Variable8,
MAX(CallTypeID) AS CallTypeID
FROM CTE
GROUP BY Variable8
--Output
/*
RecoveryKey DateTime Duration NewTransaction Variable8 CallTypeID
---------------------------------------------------------------- ----------------------- ----------- ------------------------------ ---------------------------------------------------------------- -----------
7994113912751 2018-12-04 16:51:00.000 3854 Y 152643-5657 6390
*/
如果对我的答复有任何疑问,请随时告诉我。
最好的问候,
会