简体   繁体   English

返回完成日期大于上一个记录的下一个到期日期的所有记录?

[英]return all records where the completed date is greater than a previous record's next due date?

Ok, so this feels like a complicated query due to a poor design of the database, but alas -- it is there and in need of solving. 好的,由于数据库的设计不佳,因此感觉像是一个复杂的查询,但是可惜-它确实存在并且需要解决。 The table has 5 fields: id(PK), itemNumber(FK), completedDate, completedBy(FK), nextDue. 该表有5个字段:id(PK),itemNumber(FK),completedDate,completedBy(FK),nextDue。

In the application, when someone records a task as being done, it fills in the completedDate, calculates the next due date and puts that in nextDue. 在应用程序中,当某人记录一项已完成的任务时,它将填写completedDate,计算下一个到期日并将其放入nextDue。

What I need, is a query that will basically look at a record's nextDue date, then look ahead to the next record that has the same itemNumber, and compare with that completedDate; 我需要的是一个查询,该查询基本上将查看记录的nextDue日期,然后向前查看具有相同itemNumber的下一条记录,并将其与completedDate进行比较; then return the row if the completedDate exceeds the completedBy date. 然后,如果completedDate超过completedBy日期,则返回该行。 Does that make sense? 那有意义吗? Can this be done? 能做到吗? It sounds like it would have to be done procedurally vs. pure sql. 听起来好像必须相对于纯sql在程序上完成。 TIA TIA

UDPATE: I guess I should be more specific. UDPATE:我想我应该更具体一些。 I need to use this query in a report, so if it needs to be done in VBA, I'm not sure how to get the results into that. 我需要在报表中使用此查询,因此,如果需要在VBA中完成此查询,则不确定如何获取结果。

在此处输入图片说明

I find the best way to think about these weird queries is to break 'em down into smaller manageable queries in my mind. 我发现思考这些怪异查询的最佳方法是将它们分解为较小的可管理查询。 So, I think about this as first associating and then comparing two records: The 'current' and the 'next' records. 因此,我认为这是首先关联然后比较两个记录:“当前”记录和“下一个”记录。 The first thing we want to do is associate each record (which will become the 'current') with it's corresponding 'next' record. 我们要做的第一件事是将每个记录(将成为“当前”)与其对应的“下一个”记录相关联。

So, first, a query that gets all records IDs and their corresponding "next record". 因此,首先,获取所有记录ID及其对应的“下一条记录”的查询。 This uses a less than "<" in the JOIN statement to get all of the records that are after the current record. 这将在JOIN语句中使用小于号<<来获取当前记录之后的所有记录。

SELECT
     T.id as currentId,
     MIN(A.id) as nextId
FROM
     rec_completionDate as T
          JOIN
     rec_completionDate as A ON T.itemNumber = A.itemNumber and T.id < A.id
GROUP BY
     T.id

Follow it up with a query that, using the above as a subquery, returns all of the appropriate fields for each record (current and next): 接下来是一个查询,该查询使用以上作为子查询,返回每个记录(当前和下一条)的所有适当字段:

SELECT
     current.*,
     next.*
FROM
     ( ... [subquery goes here] ...) sq
          JOIN
     rec_completionDate as current ON sq.currentId = current.id
          JOIN
     rec_completionDate as next ON sq.nextId = next.id

Lastly, we'll want to limit the results of that last query with the following WHERE clause: 最后,我们要使用以下WHERE子句来限制最后一个查询的结果:

WHERE
     next.completedDate > current.nextDue

So, putting it all together: 因此,将它们放在一起:

SELECT
     current.*,
     next.*
FROM
     ( 
     SELECT
          T.id as currentId,
          MIN(A.id) as nextId
     FROM
          rec_completionDate as T
               JOIN
          rec_completionDate as A ON T.itemNumber = A.itemNumber and T.id < A.id
     GROUP BY
          T.id
     ) sq
          JOIN
     rec_completionDate as current ON sq.currentId = current.id
          JOIN
     rec_completionDate as next ON sq.nextId = next.id
WHERE
     next.completedDate > current.nextDue

暂无
暂无

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

相关问题 如何使用 Microsoft SQL Server 2008 获取日期比今天的日期大 6 个月的所有记录? - How do I get all records where date is 6 months greater than today's date, using Microsoft SQL Server 2008? 有没有办法返回所有记录,其中特定列上的下一条记录的值来自上一条记录 + 1? - Is there a way to return all records where the next record on a specific column has a value from previous record + 1? 选择日期时间大于指定日期的记录 - Select records where datetime is greater than the specified date 如何在mysql表中选择所有大于日期的记录? - How to select all records greater than a date in mysql table? SQL:如何仅返回 1 个以前的日期进行记录,而不是所有以前的日期 - SQL: How to return just 1 previous date for a record, not all previous dates MySQL获取日期大于8个月的记录 - MySQL Get records where date is greater 8 months SQL 查询以获取 tab2 中的每个 EID 和 JOINING_DT 对应记录,这些记录只是 tab2 中的上一个、相等日期和所有更大日期的行 - SQL query to get each EID's & JOINING_DT corresponding records in tab2 which are just previous, equal date and all greater date rows from tab2 对于表a中的每条记录,返回表b中日期最接近但大于now()的记录 - Return the record with the date closest to but greater than now NOW() in table b, for each record in table a 日期超过一周的SQLite返回记录 - SQLite return records where date is more than a week old MySQL查询返回等于或大于特定日期的行,其中日期以年,月和日列分隔 - MySQL query to return rows that are equal to or greater than a certain date, where the date is separated in year, month and day columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM