简体   繁体   中英

Access SQL Subquery Criteria based on Next Record

I have a column of QualityCheckTimes. I also have a different table with the StartTimes and EndTimes of ProductionSkids.

I need a query that returns for each QualityCheckTime, the minimum SkidID and maximum SkidID based on their StartTimes and EndTimes.

Sample Data

 QCCheckTimes  
     12:00 AM  
      1:00 AM  
      2:00 AM  

 SkidID   SkidStartTime  SkidEndTime  
      1        12:05 AM     12:20 AM  
      2        12:21 AM     12:40 AM  
      3        12:41 AM     12:50 AM  
      4        12:51 AM      1:06 AM

Expected Output:

 QCCheckTimes MinSkidID  MaxSkidID 
     12:00 AM     Skid1      Skid3   
      1:00 AM     Skid4        ...  
      2:00 AM       ...  

I've tried a few things, but the crux of it is that I need to find a way to get all the matching Skid Times between two QualityCheck times, with those QualityTimes being on separate rows.

SELECT...
WHERE [SkidStartDateTime] >= [QualitySamples_tbl].[SampleDateTime]
AND [SkidEndDateTime] < NEXT?? [QualitySamples_tbl].[SampleDateTime]);

You can use subqueries and Hour() function to achieve this.

Try this:

  • table1 contains: QCCheckTimes

  • table2 Contains: SkidID,SkidStartTime,SkidEndTime

Code:

SELECT table1.qcchecktimes, 
       Iif([minofid] IS NULL, "...", "skid" & [minofid])  AS MinSkidID, 
       Iif([c].[maxofid] = [b].[minofid], "...", 
       Iif([maxofid] IS NULL, "...", "skid" & [maxofid])) AS MaxSkidID 
FROM   (table1 
        LEFT JOIN (SELECT table1.qcchecktimes, 
                          Min(table2.skidid) AS MinOfID 
                   FROM   table1, 
                          table2 
                   WHERE  (( ( Hour([skidendtime]) ) = Hour([qcchecktimes]) )) 
                   GROUP  BY table1.qcchecktimes) AS b 
               ON table1.qcchecktimes = b.qcchecktimes) 
       LEFT JOIN (SELECT table1.qcchecktimes, 
                         Max(table2.skidid) AS MaxOfid 
                  FROM   table1, 
                         table2 
                  WHERE  (( ( Hour([skidendtime]) ) = Hour([qcchecktimes]) )) 
                  GROUP  BY table1.qcchecktimes) AS c 
              ON table1.qcchecktimes = c.qcchecktimes 
GROUP  BY table1.qcchecktimes, 
          Iif([minofid] IS NULL, "...", "skid" & [minofid]), 
          Iif([c].[maxofid] = [b].[minofid], "...", 
          Iif([maxofid] IS NULL, "...", "skid" & [maxofid])); 

I don't use access but in SQL Server you have two functions to achieve this query LAG and LEAD , previous and next row, you can see a tutorial here http://www.c-sharpcorner.com/UploadFile/f82e9a/lag-and-lead-functions-in-sql-server/

so in SQL you can do something like this (this example is using integers not time)

  ----------------your single table-----------------
  declare @a table(id int)
  insert into @a
  select 1 union
  select 5 union
select 10 union
select 14 union
select 17 union
select 20 
----------------table with ranges-----------------
declare @b table (start int, finish int)
insert into @b
select 1,4 union
select 2,5 union
select 5,8 union
select 10,15 
----------------Aux Table--------------------------
declare @a_PlusNext table (id int, idNext int)
insert into @a_PlusNext
SELECT id,LEAD(id) OVER(ORDER BY id) nextId from @A

----------------------Final Query------------------
SELECT 
*
FROM @a_PlusNext
INNER JOIN @b on start >= id and finish <= idNext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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