简体   繁体   English

将同一张表的连续行中的同一列与多个ID进行比较

[英]Compare same column in consecutive rows in same table with multiple ID's

I have a user request for a report and I'm too new to SQL programming to know how to approach it. 我有一个用户要报告的请求,而对于SQL编程我还是太陌生,不知道如何处理它。

My user wants to know for each Staff ID what is the min, avg and max number of days between visits. 我的用户想为每个员工ID知道两次访问之间的最小,平均和最大天数。 What I don't know how to figure out is the number of days between Visit 1 and Visit 2; 我不知道如何计算出访问1和访问2之间的天数; Visit 2 and Visit 3, etc., for each Person ID. 每个人员ID的访问2和访问3,依此类推。 Some Person ID's only have one visit, others (most) have multiple visits (up to 26). 某些人员ID只有一次访问,而其他人(大多数)有多次访问(最多26次)。 Here is a snapshot of some data (the full dataset is over 14k records): 这是一些数据的快照(完整的数据集超过14k条记录):

PersonID    VisitNo    StaffID    VisitDate  
161          1         42344      06/19/2018
163          1         32987      05/14/2018
163          2         32987      09/17/2018
193          1         42344      04/09/2018
193          2         42344      07/18/2018
193          1         33865      07/18/2018  
207          1         32987      10/10/2018
207          2         32987      11/05/2018 
329          1         42344      04/15/2018
329          2         42344      05/23/2018
329          3         42344      06/10/2018
329          4         42344      07/18/2018
329          1         33865      06/30/2018
329          2         33865      09/14/2018

My research found a lot of references to comparing rows in the same table and I figured out how to compare one visit to the next for a single PersonID using a self join and datadiff, but how do I get from one PersonID to the next, or skip those PersonID's with only 1 visit? 我的研究发现,有很多参考资料可以用来比较同一张表中的行,并且我发现了如何使用自我连接和datadiff对单个PersonID进行一次访问与下一次访问的比较,但是如何从一个PersonID进行到下一个访问,或者只需1次访问就跳过那些PersonID? Or a PersonID who has visits with multiple StaffId's? 还是访问了多个StaffId的PersonID?

Any ideas/suggestions are greatly appreciated as I have two requests that will benefit. 任何想法/建议都将不胜感激,因为我有两个要求将从中受益。

You can use analytic function LEAD (myvar,1) OVER () 您可以使用分析函数LEAD (myvar,1) OVER ()

example from https://www.techonthenet.com/sql_server/functions/lead.php 来自https://www.techonthenet.com/sql_server/functions/lead.php的示例

SELECT dept_id, last_name, salary,
LEAD (salary,1) OVER (ORDER BY salary) AS next_highest_salary
FROM employees;

For the average number of days, you can just use aggregation: 对于平均天数,您可以使用聚合:

select personid,
       (datediff(day, min(visitdate), max(visitdate)) * 1.0 / nullif(count(*) - 1, 0)
from t
group by personid;

I used SQL Server syntax, but the same idea holds in any database. 我使用了SQL Server语法,但是在任何数据库中都存在相同的想法。 The average is the maximum minus the minimum divided by one less than the number of visits. 平均值是最大值减去最小值,再除以访问次数减去一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM