Sql 按列和日期范围获取最大行数
我正在尝试根据一列和另一列的日期范围获取唯一的行列表。以下是示例数据:Sql 按列和日期范围获取最大行数,sql,tsql,Sql,Tsql,我正在尝试根据一列和另一列的日期范围获取唯一的行列表。以下是示例数据: id CustomerNum ServiceDate ------------------------------------------------ 4406290 000000000066 2017-02-17 13:03:00.000 4406294 000000000066 2017-02-17 13:07:00.000 4406295 000000000066 2017-02-17
id CustomerNum ServiceDate
------------------------------------------------
4406290 000000000066 2017-02-17 13:03:00.000
4406294 000000000066 2017-02-17 13:07:00.000
4406295 000000000066 2017-02-17 13:09:00.000
4406295 000000000066 2017-02-09 13:09:00.000
4406352 000000000066 2017-01-17 13:12:00.000
4406369 000000000066 2017-03-17 13:16:00.000
4582381 000000ABC102 2016-03-22 14:48:00.017
4589037 000000ABC102 2016-07-23 14:54:11.223
4625101 000009983148 2017-03-30 15:21:11.283
4625162 000005555398 2017-01-30 11:22:20.907
4625165 000005555398 2017-03-30 12:22:20.907
4625168 000005555398 2017-03-30 15:22:20.907
我需要按CustomerNum分组,然后按ServiceDate分组,但前提是ServiceDate在7天内。我想要分组的最大(最新)行。因此,结果应该是:
id CustomerNum ServiceDate
------------------------------------------------
4406295 000000000066 2017-02-17 13:09:00.000
4406295 000000000066 2017-02-09 13:09:00.000
4406352 000000000066 2017-01-17 13:12:00.000
4406369 000000000066 2017-03-17 13:16:00.000
4582381 000000ABC102 2016-03-22 14:48:00.017
4589037 000000ABC102 2016-07-23 14:54:11.223
4625101 000009983148 2017-03-30 15:21:11.283
4625162 000005555398 2017-01-30 11:22:20.907
4625168 000005555398 2017-03-30 15:22:20.907
我尝试了以下方法:
WITH cte
AS (
SELECT *
,ROW_NUMBER() OVER (
PARTITION BY CustomerNum ORDER BY ServiceDate DESC
) AS rn
FROM CustomerTransactions
)
SELECT *
FROM cte
WHERE rn = 1
但这给了我所有CustomerNum上的行号,而不是在满足日期范围后从1开始的行号
我知道我错过了什么。有什么想法吗?谢谢。对于此问题,您希望使用
lag()
和累积和。使用lag确定每个组的起始位置,然后使用累积和分配组:
select sum(case when prev_ServiceDate > dateadd(day, -7, ServiceDate) then 0 else 1 end) over
(partition by CustomerNum order by ServiceDate) as grp
from (select ct.*,
lag(ServiceDate) over (partition by CustomerNum order by ServiceDate) as prev_ServiceDate
from CustomerTransactions ct
) ct;
然后,您可以使用聚合对组进行汇总:
select CustomerNum, min(ServiceDate), max(ServiceDate)
from (select sum(case when prev_ServiceDate > dateadd(day, -7, ServiceDate) then 0 else 1 end) over
(partition by CustomerNum order by ServiceDate) as grp
from (select ct.*,
lag(ServiceDate) over (partition by CustomerNum order by ServiceDate) as prev_ServiceDate
from CustomerTransactions ct
) ct
) ct
group by CustomerNum, grp
对于这个问题,您需要使用
lag()
和累积和。使用lag确定每个组的起始位置,然后使用累积和分配组:
select sum(case when prev_ServiceDate > dateadd(day, -7, ServiceDate) then 0 else 1 end) over
(partition by CustomerNum order by ServiceDate) as grp
from (select ct.*,
lag(ServiceDate) over (partition by CustomerNum order by ServiceDate) as prev_ServiceDate
from CustomerTransactions ct
) ct;
然后,您可以使用聚合对组进行汇总:
select CustomerNum, min(ServiceDate), max(ServiceDate)
from (select sum(case when prev_ServiceDate > dateadd(day, -7, ServiceDate) then 0 else 1 end) over
(partition by CustomerNum order by ServiceDate) as grp
from (select ct.*,
lag(ServiceDate) over (partition by CustomerNum order by ServiceDate) as prev_ServiceDate
from CustomerTransactions ct
) ct
) ct
group by CustomerNum, grp
仅当服务日期在7天之内时,您所说的
是什么意思?例如,如果客户11111的服务日期为2017年7月1日,其中一个为2017年7月3日,那么这两个行都是唯一的行,但如果他们的服务日期为2017年8月3日,这是2017年7月3日后的7天内的结果,因此这不是唯一的结果。仅当ServiceDate在7天内时,您所说的是什么意思?例如,如果客户11111的服务日期为2017年7月1日,其中一行为2017年7月3日,那么这两行都是唯一的行,但如果他们在2017年8月3日有另一行,这将在2017年7月3日后的7天内发生,因此这不是一个独特的结果。经过一些调整,这就像一个冠军。非常感谢。通过一些调整,这就像一个冠军。非常感谢。