mtairhead
Registered User.
- Local time
- Today, 18:36
- Joined
- Oct 17, 2003
- Messages
- 138
Hey, all...
I'm having some difficulty with a subquery. I'm using a subquery to find a previous record's value. It works, with a few exceptions. Here's what I have:
As you can probably extrapolate with enough scrutiny, these are site event logs. The query I'm building would return the time and date (separate fields) of each event, along with the time (I'll add the date in eventually) of the previous event logged by the previous user.
This query works...mostly. The problem comes when I have a visitor whose time extends from the AM to the PM. Here are some example return values:
+------------------------------------------+
|PatronID | Date | Time |Previous Time|
|------------------------------------------+
|100 |1/1/08|10:00AM |-- (Blank)
|100 |1/1/08|10:05AM |10:00AM
|100 |1/1/08|10:10AM |10:05AM
|200 |1/1/08|11:00AM | 2:45PM
|200 |1/1/08|11:15AM |11:00AM
|300 |1/1/08|11:30AM |--(Blank)
|300 |1/1/08|11:45AM |11:30AM
|200 |1/1/08| 2:45PM |--(Blank)
|200 |1/1/08| 2:50PM | 2:45PM
-------------------------------------------+
The events of patron 100 and 300 are flawless. The results for patron 200 are troubling. Access is returning 2:45PM, a future time, as the closest previous time. My SQL asks for a smaller (or equal) date and a smaller time. Then the patron's 2:45 event returns nothing for the previous time, despite the event logged at 11:15AM. A proper query would return 11:45AM.
Any insight is appreciated.
Thanks,
Andrew
I'm having some difficulty with a subquery. I'm using a subquery to find a previous record's value. It works, with a few exceptions. Here's what I have:
PHP:
SELECT tblEventLogFile_Clean.Date, tblEventLogFile_Clean.Time, (SELECT TOP 1 previousRecord.Time
FROM tblEventLogFile_Clean as previousRecord
WHERE previousRecord.Time < tblEventLogFile_Clean.Time
AND previousRecord.Date <= tblEventLogFile_Clean.Date
AND previousRecord.PatronID = tblEventLogFile_Clean.PatronID
ORDER BY Date DESC, Time DESC) AS Expr1
FROM tblEventLogFile_Clean;
As you can probably extrapolate with enough scrutiny, these are site event logs. The query I'm building would return the time and date (separate fields) of each event, along with the time (I'll add the date in eventually) of the previous event logged by the previous user.
This query works...mostly. The problem comes when I have a visitor whose time extends from the AM to the PM. Here are some example return values:
+------------------------------------------+
|PatronID | Date | Time |Previous Time|
|------------------------------------------+
|100 |1/1/08|10:00AM |-- (Blank)
|100 |1/1/08|10:05AM |10:00AM
|100 |1/1/08|10:10AM |10:05AM
|200 |1/1/08|11:00AM | 2:45PM
|200 |1/1/08|11:15AM |11:00AM
|300 |1/1/08|11:30AM |--(Blank)
|300 |1/1/08|11:45AM |11:30AM
|200 |1/1/08| 2:45PM |--(Blank)
|200 |1/1/08| 2:50PM | 2:45PM
-------------------------------------------+
The events of patron 100 and 300 are flawless. The results for patron 200 are troubling. Access is returning 2:45PM, a future time, as the closest previous time. My SQL asks for a smaller (or equal) date and a smaller time. Then the patron's 2:45 event returns nothing for the previous time, despite the event logged at 11:15AM. A proper query would return 11:45AM.
Any insight is appreciated.
Thanks,
Andrew