简体   繁体   English

如何联接同一表的下一个日期值

[英]How to join the next date value of the same table

I have a table in SQL with the following fields: 我在SQL中有一个具有以下字段的表:

在此处输入图片说明

The timestamp field will have all the punches that an employee has in a day. timestamp字段将包含员工一天中的所有工作。

So having the following data: 因此,具有以下数据:

在此处输入图片说明

I need to create 2 diferent queries. 我需要创建2个不同的查询。

  1. need to select all the IN timestamps with their corresponding next OUT timestamp 需要选择所有IN时间戳及其对应的下一个OUT时间戳

  2. need to select all the OUT timestamps with their corresponding previous IN timestamp 需要选择所有OUT时间戳及其对应的先前IN时间戳

So, in the first query, I should get the following: 因此,在第一个查询中,我应该得到以下内容:

在此处输入图片说明

In the second query, I should get the following: 在第二个查询中,我应该得到以下内容:

在此处输入图片说明

Any clue on how to build such queries? 关于如何建立此类查询的任何线索?

HERE IS THE Fiddle : http://sqlfiddle.com/#!6/a137d/1 小提琴在这里http ://sqlfiddle.com/#!6/a137d /1

This looks like nice example for usage of LEAD, LAG ANALYTIC functions in SQL 2012. 这看起来像是在SQL 2012中使用LEAD,LAG ANALYTIC函数的好例子。

SELECT * FROM
(
  SELECT EMPLOYEEID, TIMESTAMP,
  LEAD(timestamp) OVER (ORDER BY TIMESTAMP
  ) OUTTIMESTAMP, ACCESSCODE

  FROM [dbo].[employee_attendance]
  WHERE EMPLOYEEID =4 

) T 
where T.ACCESSCODE ='IN'

second query 第二个查询

SELECT * FROM
(
  SELECT EMPLOYEEID, TIMESTAMP,
  LAG(timestamp) OVER (ORDER BY TIMESTAMP
  ) INTIMESTAMP, ACCESSCODE

  FROM [dbo].[employee_attendance]
  WHERE EMPLOYEEID =4 

) T 
where T.ACCESSCODE ='OUT'

I believe this is what you're looking for. 我相信这就是您要寻找的。 These queries should work on most DBMSs. 这些查询应适用于大多数DBMS。

First 第一

SELECT ea1.employeeid, ea1.timestamp AS instamp, ea2.timestamp AS outstamp
FROM employee_attendance ea1
LEFT JOIN employee_attendance ea2 
ON ea2.employeeid=ea1.employeeid 
AND ea2.accesscode = 'OUT' 
AND ea2.timestamp = (
    SELECT MIN(ea3.timestamp)
    FROM employee_attendance ea3
    WHERE ea3.timestamp > ea1.timestamp
    AND ea3.employeeid = ea1.employeeid
)
WHERE ea1.accessCode = 'IN'
AND ea1.employeeid = 4;

Second 第二

SELECT ea1.employeeid, ea1.timestamp AS outstamp, ea2.timestamp AS instamp
FROM employee_attendance ea1
LEFT JOIN employee_attendance ea2 
ON ea2.employeeid=ea1.employeeid 
AND ea2.accesscode = 'IN' 
AND ea2.timestamp = (
    SELECT MIN(ea3.timestamp)
        FROM employee_attendance ea3
        WHERE ea3.timestamp < ea1.timestamp
        AND ea3.employeeid = ea1.employeeid
        AND ea3.timestamp > ISNULL((
            SELECT MAX(ea4.timestamp)
            FROM employee_attendance ea4
            WHERE ea4.accesscode = 'OUT'
            AND ea4.timestamp < ea1.timestamp
            AND ea4.employeeid = ea1.employeeid
        ), '2000-1-1')
)
WHERE ea1.accessCode = 'OUT'
AND ea1.employeeid = 4;

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

相关问题 如何获取表中下一个和下一个日期的值以及与另一个表的外部连接 - How to get the values for the next and next next date in a table and outer join with another table 连接两个表,其中表 A 具有日期值,需要在 A 中的日期下方找到 B 中的下一个日期 - Join two tables where table A has a date value and needs to find the next date in B below the date in A Select 下一个可用日期并加入另一个表 - Select next available date and join to another table 如何通过相同条件值连接另一个表中的值 - how to join value from another table by same condition value 如何在MySQL中联接两个或多个表的最小值和日期 - How to join two or multiple table behalf of min value and and date in Mysql 如果表中的下一条记录与特定字段中的值相同,如何删除一条记录 - How to delete a record if the next record in table as the same value in specific field 加入同一表格的匹配日期范围 - Join to same table match date ranges 如何应用一对多表连接并获取同一列中的值? - How to apply one to many table join and get the value in same column? Postgres:如何从同一表联接最接近的值 - Postgres: how to join closest value from the same table SQL 如何从一个列表中连接相同的值然后求和 - SQL how to join same value from one column table and then sum it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM