Results 1 to 9 of 9
  1. #1
    Lance1578 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    4

    Post Inserting and Processing Data from one table to another table

    Im trying to process the data from tblTempData to tblPData using the ff sql:
    Code:
    INSERT INTO tblPData ( fldMacID, fldYear, fldMonth, fldDay,fldTimeIn1,fldTimeOut1,fldTimeIn2,fldTimeOut2 )
    SELECT 
     T1.fldMachineId, Year( T1.theDate),Month( T1.theDate),Day( T1.theDate),
     T2.fldLogDate - int(T2.fldLogDate) as TimeIn1,
     T3.fldLogDate - int(T3.fldLogDate) as TimeOut1,
     T4.fldLogDate - int(T4.fldLogDate) as TimeIn2,
     T5.fldLogDate - int(T5.fldLogDate) as TimeOut2
    FROM (((
    (Select Distinct tblTempData.fldMachineId,int(tblTempData.fldLogDate)  as theDate from tblTempData) as T1
        LEFT JOIN tblTempData as T2 ON (T1.fldMachineId = T2.fldMachineId AND T1.theDate = Int(T2.flgLogDate) AND T2.LogStatus = 1 AND Hour(T2.fldTimeLog) < 11)     )
        LEFT JOIN tblTempData as T3 ON (T1.fldMachineId = T3.fldMachineId AND T1.theDate = Int(T3.flgLogDate) AND T3.LogStatus = 2 AND Hour(T3.fldTimeLog) < 15)     )
        LEFT JOIN tblTempData as T4 ON (T1.fldMachineId = T4.fldMachineId AND T1.theDate = Int(T4.flgLogDate) AND T4.LogStatus = 1 AND Hour(T4.fldTimeLog) >= 11)     )
        LEFT JOIN tblTempData as T5 ON (T1.fldMachineId = T5.fldMachineId AND T1.theDate = Int(T5.flgLogDate) AND T5.LogStatus = 2 AND Hour(T5.fldTimeLog) >= 15)
    When I run that code in MS Access Query Tools it didn't work as expected. And I cant really get it right.
    I really appreciate any help on this.


    Please see image for more info on my table and fields
    Click image for larger version. 

Name:	Capture_020.jpg 
Views:	21 
Size:	105.1 KB 
ID:	31966

    Thank you very much.

  2. #2
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Don't know if there's a sql guru lurking here that has a solution. Based on my interpretation I would use code, but I don't get why machine 2 on 9/20 with only 2 times isn't time1in and time1out.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Lance1578 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    4
    Quote Originally Posted by Micron View Post
    Don't know if there's a sql guru lurking here that has a solution. Based on my interpretation I would use code, but I don't get why machine 2 on 9/20 with only 2 times isn't time1in and time1out.
    Hi:
    User 2 did not timeout1 and TimeIn2. He only TimeIn in the morning n TimeOut in the afternoon.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    Did you test the nested SELECT as a standalone query object? are the expected records retrieved?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    At first I figured that 1 is IN and 2 is OUT, and if one occurs before noon it's Time1 but is Time2 if it's after noon. However, 9/19 doesn't support that theory so I'm not grasping the pattern.

  6. #6
    Lance1578 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    4
    The data comes from the biometric machine with the following format:
    2 2017-7-20 7:05:00 1 0
    2 2017-7-20 11:25:00 1 1
    2 2017-7-20 12:30:00 1 0
    2 2017-7-20 17:45:00 1 1
    .....
    almost 4k lines per month

    LogStatus (TimeIn) = 1 (whether it is TimeIN1 or TimeIN2) once the user login it will mark 1
    LogStatus (TimeOut) = 2 (whether it is TimeOUT1 or TimeOUT2) once the user logOut it will mark 2

    ex1:
    MacID | Year | Month | Day | TimeIn1 | TimeOUT1 | TimeIN2 | TimeOUT2
    2 2017 7 20 7:05 11:25 12:30 17:45
    2 2017 7 21 6:59 13:03 17:20
    2 2017 7 22 11:45 17:56
    2 2017 7 23 12:01

    Sometimes the user forgot to login or logout so the field is blank.

    Using this code ill be able to collect all TimeIn and TimeOut.. Sadly it takes me 4 Tables
    Code:
    // Insert to tblTimeIN1
    INSERT INTO tblTimeIN1 ( fldMacID, fldYear, fldMonth, fldDay, fldTimeIn1 )
    SELECT tblTempData.fldMachineId, tblTempData.fldLogDate, tblTempData.fldLogDate,tblTempData.fldLogDate, tblTempData.fldLogDate
    FROM tblTempData
    WHERE (((Format([tblTempData].[fldLogDate],"Short Time"))<"11:00")and tbltempdata.fldLogStatus = 1))
    ORDER BY tblTempData.fldMachineId, Day(tblTempData.fldLogDate);
    
    // Insert to tblTimeOut1
    INSERT INTO tblTimeOut1 ( fldMacID, fldYear, fldMonth, fldDay, fldTimeOut1, fldLogStatus )
    SELECT tblTempData.fldMachineId, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogStatus
    FROM tblTempData
    WHERE Format([tblTempData].[fldLogDate],"hh:mm") >"11:00" and Format([tblTempData].[fldLogDate],"hh:mm") < "14:00" and tbltempdata.fldLogStatus = 2
    ORDER BY tblTempData.fldMachineId, Day(tblTempData.fldLogDate);
    
    // Insert to tblTimeIN2
    INSERT INTO tblTimeIN2 ( fldMacID, fldYear, fldMonth, fldDay, fldTimeIn2, fldLogStatus )
    SELECT tblTempData.fldMachineId, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogStatus
    FROM tblTempData
    WHERE Format([tblTempData].[fldLogDate],"hh:mm") >"11:30" and Format([tblTempData].[fldLogDate],"hh:mm") < "14:00" and tbltempdata.fldLogStatus = 1
    ORDER BY tblTempData.fldMachineId, Day(tblTempData.fldLogDate);
    
    // Insert to tblTimeOut2
    INSERT INTO tblTimeOut2 ( fldMacID, fldYear, fldMonth, fldDay, fldTimeOut2, fldLogStatus )
    SELECT tblTempData.fldMachineId, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogDate, tblTempData.fldLogStatus
    FROM tblTempData
    WHERE (((Format([tblTempData].[fldLogDate],"Short Time"))>"16:00" and tbltempdata.fldLogStatus = 2))
    ORDER BY tblTempData.fldMachineId, Day(tblTempData.fldLogDate);
    and have another table to collect all TimeIN1, TimeOUT1, TimeIN2, TimeOUT2.
    as much as possible i would like to build all process in sql side.


    Lance

  7. #7
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Your incoming data does not specify whether it is time 1, time 2, etc. I would highly recommend that you do not do that. Create one record per time in/out, regardless how many there are for a date. Does the incoming data not allow someone to do more than 2 logs per day? It sounds very risky to me.

    As Micron says, so easy to do in code. Let us know if you need help with that.

  8. #8
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    so easy to do in code.
    Except I'd still have no idea where to put which times because it seems arbitrary (per posts 2 and 5)
    (whether it is TimeIN1 or TimeIN2);(whether it is TimeOUT1 or TimeOUT2)
    Flip a coin?

  9. #9
    Lance1578 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    4
    Thanks for the suggestion. I keep you update.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. inserting query into table
    By kiranair in forum Access
    Replies: 1
    Last Post: 06-08-2016, 08:01 AM
  2. Inserting Data to Access Table (Local) from MySQL table (Net)
    By gambit1430 in forum Import/Export Data
    Replies: 2
    Last Post: 09-26-2014, 01:34 AM
  3. inserting a table
    By vaseemali in forum Forms
    Replies: 3
    Last Post: 08-21-2011, 02:03 PM
  4. Table Setup advice for invoice processing...
    By Delta729 in forum Database Design
    Replies: 1
    Last Post: 11-23-2010, 11:52 AM
  5. Inserting values from one table to another
    By dada in forum Programming
    Replies: 3
    Last Post: 10-10-2010, 06:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums