确定客户的访问次数和停留时间
Determine Number of Visits and Length of Stay by Client
我有一组只有 ClientID 和 ServiceDate 的客户记录
我想确定每次访问的访问次数和停留时间
我已经在 MS Access 中创建了一些代码并阅读了搜索此问题时推荐的几个类似问题以及从 Google 搜索
返回的其他建议
当前代码:
SELECT
a.Client,
a.ServiceDate,
DateAdd ( "d", -1, a.ServiceDate ) AS [Prior],
DateAdd ( "d", 1, a.ServiceDate ) AS [Next], ( SELECT COUNT(*)
FROM Services_Table b
WHERE a.Client = b.Client
AND DateAdd ( "d", -1, a.ServiceDate ) = b.ServiceDate ) AS [Index]
FROM Services_Table AS a
ORDER BY a.Client, a.ServiceDate;
输入数据:
Client ServiceDate
1 8/4/2018
1 8/5/2018
1 8/6/2018
6 7/11/2018
6 7/12/2018
6 7/17/2018
6 7/18/2018
6 7/19/2018
6 7/30/2018
6 7/31/2018
6 8/1/2018
6 8/2/2018
15 1/13/2019
15 1/14/2019
5 9/13/2018
5 9/14/2018
5 9/15/2018
当前输出:
Clt ServiceDate Prior Next Index
1 8/4/2018 8/3/2018 8/5/2018 0
1 8/5/2018 8/4/2018 8/6/2018 1
1 8/6/2018 8/5/2018 8/7/2018 1
5 9/13/2018 9/12/2018 9/14/2018 0
5 9/14/2018 9/13/2018 9/15/2018 1
5 9/15/2018 9/14/2018 9/16/2018 1
6 7/11/2018 7/10/2018 7/12/2018 0
6 7/12/2018 7/11/2018 7/13/2018 1
6 7/17/2018 7/16/2018 7/18/2018 0
6 7/18/2018 7/17/2018 7/19/2018 1
6 7/19/2018 7/18/2018 7/20/2018 1
6 7/30/2018 7/29/2018 7/31/2018 0
6 7/31/2018 7/30/2018 8/1/2018 1
6 8/1/2018 7/31/2018 8/2/2018 1
6 8/2/2018 8/1/2018 8/3/2018 1
15 1/13/2019 1/12/2019 1/14/2019 0
15 1/14/2019 1/13/2019 1/15/2019 1
期望的结果:
Clt Start End Length
1 8/4/2018 8/6/2018 3
5 9/13/2018 9/15/2018 3
6 7/11/2018 7/12/2018 2
6 7/17/2018 7/19/2018 3
6 7/30/2018 8/2/2018 4
15 1/13/2019 1/14/2019 2
让我们从确定起点开始:
开始日期是没有连续的前一个日期的日期,因此:
SELECT Client, ServiceDate
FROM Services_Table t
WHERE NOT EXISTS(SELECT 1 FROM Services_Table s WHERE s.Client = t.Client AND s.ServiceDate = DateAdd("d", -1, t.ServiceDate))
同样,我们可以识别两端:
SELECT Client, ServiceDate
FROM Services_Table t
WHERE NOT EXISTS(SELECT 1 FROM Services_Table s WHERE s.Client = t.Client AND s.ServiceDate = DateAdd("d", 1, t.ServiceDate))
然后,我们可以将开始和结束连接在一起,select最接近开始的结束日期:
SELECT Start.Client, Start.ServiceDate As Start, Min(End.ServiceDate) As End, Min(End.ServiceDate) - Start.ServiceDate As Length
FROM
(
SELECT Client, ServiceDate
FROM Services_Table t
WHERE NOT EXISTS(SELECT 1 FROM Services_Table s WHERE s.Client = t.Client AND s.ServiceDate = DateAdd("d", -1, t.ServiceDate))
) Start
INNER JOIN (
SELECT Client, ServiceDate
FROM Services_Table t
WHERE NOT EXISTS(SELECT 1 FROM Services_Table s WHERE s.Client = t.Client AND s.ServiceDate = DateAdd("d", 1, t.ServiceDate))
) End ON (Start.Client = End.Client AND Start.ServiceDate <= End.ServiceDate)
GROUP BY Start.Client, Start.ServiceDate
我有一组只有 ClientID 和 ServiceDate 的客户记录
我想确定每次访问的访问次数和停留时间
我已经在 MS Access 中创建了一些代码并阅读了搜索此问题时推荐的几个类似问题以及从 Google 搜索
返回的其他建议当前代码:
SELECT
a.Client,
a.ServiceDate,
DateAdd ( "d", -1, a.ServiceDate ) AS [Prior],
DateAdd ( "d", 1, a.ServiceDate ) AS [Next], ( SELECT COUNT(*)
FROM Services_Table b
WHERE a.Client = b.Client
AND DateAdd ( "d", -1, a.ServiceDate ) = b.ServiceDate ) AS [Index]
FROM Services_Table AS a
ORDER BY a.Client, a.ServiceDate;
输入数据:
Client ServiceDate
1 8/4/2018
1 8/5/2018
1 8/6/2018
6 7/11/2018
6 7/12/2018
6 7/17/2018
6 7/18/2018
6 7/19/2018
6 7/30/2018
6 7/31/2018
6 8/1/2018
6 8/2/2018
15 1/13/2019
15 1/14/2019
5 9/13/2018
5 9/14/2018
5 9/15/2018
当前输出:
Clt ServiceDate Prior Next Index
1 8/4/2018 8/3/2018 8/5/2018 0
1 8/5/2018 8/4/2018 8/6/2018 1
1 8/6/2018 8/5/2018 8/7/2018 1
5 9/13/2018 9/12/2018 9/14/2018 0
5 9/14/2018 9/13/2018 9/15/2018 1
5 9/15/2018 9/14/2018 9/16/2018 1
6 7/11/2018 7/10/2018 7/12/2018 0
6 7/12/2018 7/11/2018 7/13/2018 1
6 7/17/2018 7/16/2018 7/18/2018 0
6 7/18/2018 7/17/2018 7/19/2018 1
6 7/19/2018 7/18/2018 7/20/2018 1
6 7/30/2018 7/29/2018 7/31/2018 0
6 7/31/2018 7/30/2018 8/1/2018 1
6 8/1/2018 7/31/2018 8/2/2018 1
6 8/2/2018 8/1/2018 8/3/2018 1
15 1/13/2019 1/12/2019 1/14/2019 0
15 1/14/2019 1/13/2019 1/15/2019 1
期望的结果:
Clt Start End Length
1 8/4/2018 8/6/2018 3
5 9/13/2018 9/15/2018 3
6 7/11/2018 7/12/2018 2
6 7/17/2018 7/19/2018 3
6 7/30/2018 8/2/2018 4
15 1/13/2019 1/14/2019 2
让我们从确定起点开始:
开始日期是没有连续的前一个日期的日期,因此:
SELECT Client, ServiceDate
FROM Services_Table t
WHERE NOT EXISTS(SELECT 1 FROM Services_Table s WHERE s.Client = t.Client AND s.ServiceDate = DateAdd("d", -1, t.ServiceDate))
同样,我们可以识别两端:
SELECT Client, ServiceDate
FROM Services_Table t
WHERE NOT EXISTS(SELECT 1 FROM Services_Table s WHERE s.Client = t.Client AND s.ServiceDate = DateAdd("d", 1, t.ServiceDate))
然后,我们可以将开始和结束连接在一起,select最接近开始的结束日期:
SELECT Start.Client, Start.ServiceDate As Start, Min(End.ServiceDate) As End, Min(End.ServiceDate) - Start.ServiceDate As Length
FROM
(
SELECT Client, ServiceDate
FROM Services_Table t
WHERE NOT EXISTS(SELECT 1 FROM Services_Table s WHERE s.Client = t.Client AND s.ServiceDate = DateAdd("d", -1, t.ServiceDate))
) Start
INNER JOIN (
SELECT Client, ServiceDate
FROM Services_Table t
WHERE NOT EXISTS(SELECT 1 FROM Services_Table s WHERE s.Client = t.Client AND s.ServiceDate = DateAdd("d", 1, t.ServiceDate))
) End ON (Start.Client = End.Client AND Start.ServiceDate <= End.ServiceDate)
GROUP BY Start.Client, Start.ServiceDate