Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
knox203
Starting Member
31 Posts |
Posted - 2008-09-25 : 11:18:26
|
| Hello all! Can anyone point me in the right direction query wise as to get the first, and last record on a per day basis? I have a database filled with hundreds of usernames, along with thousands of entries per username. I would like my query to return the first, and last record for each user, per day within a given time range. Thanks for your help!- Adam |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-25 : 11:21:25
|
| [code]SELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY Username ORDER BY datefield) AS FSeq,ROW_NUMBER() OVER(PARTITION BY Username ORDER BY datefield DESC) AS BSeq,*FROM YourTableWHERE datefield BETWEEN @RangeStart AND @RangeEnd)tWHERE t.FSeq=1OR t.BSeq=1[/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-25 : 11:25:07
|
What is first and last?By ID, or by some date? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-25 : 11:26:56
|
[code]SELECT s.*FROM Table1 AS sINNER JOIN ( SELECT UserID, MIN(colDate) AS minDate, MAX(colDate) AS maxDate FROM Table1 WHERE colDate >= '20080101' AND colDate < '20090101' GROUP BY UserID, DATEDIFF(DAY, '19000101', colDate) ) AS w ON w.UserID = s.UserIDWHERE s.colDate IN (w.minDate, w.maxDate)[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
knox203
Starting Member
31 Posts |
Posted - 2008-09-25 : 11:30:08
|
| Sorry I didn't clarify, I have a timestamp column that I would like it to read. Does this help?* Checking submitted queries, thanks guys! * |
 |
|
|
knox203
Starting Member
31 Posts |
Posted - 2008-09-25 : 11:31:32
|
Maybe this will help as well, here's my base code along with the necessary JOINS:SELECT *FROM Driver INNER JOIN Job ON Driver.DriverID = Job.DriverID INNER JOIN DispatchGroup ON Driver.DispatchGroupID = DispatchGroup.DispatchGroupID INNER JOIN JobStop ON Job.JobID = JobStop.JobID |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-25 : 11:34:04
|
You want ALL columns? Really? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
knox203
Starting Member
31 Posts |
Posted - 2008-09-25 : 11:35:59
|
quote: Originally posted by Peso You want ALL columns? Really? E 12°55'05.63"N 56°04'39.26"
Sorry, no, not at all... I just need "Driver.DriverCode" along with the first, and last record DateTime for each day. |
 |
|
|
knox203
Starting Member
31 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-25 : 11:37:48
|
quote: Originally posted by knox203 Sorry I didn't clarify, I have a timestamp column that I would like it to read. Does this help?* Checking submitted queries, thanks guys! *
Use timestamp column in place where i specified datefield to get first and last for particular day value in timestamp. |
 |
|
|
knox203
Starting Member
31 Posts |
Posted - 2008-09-25 : 11:58:19
|
quote: Originally posted by visakh16
quote: Originally posted by knox203 Sorry I didn't clarify, I have a timestamp column that I would like it to read. Does this help?* Checking submitted queries, thanks guys! *
Use timestamp column in place where i specified datefield to get first and last for particular day value in timestamp.
Here's what I currently have:SELECT *FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Driver.DriverCode ORDER BY JobStop.ArriveDateTime) AS FSeq, ROW_NUMBER() OVER (PARTITION BY Driver.DriverCodeORDER BY JobStop.ArriveDateTime DESC) AS BSeq, Driver.DriverCode, JobStop.ArriveDateTimeFROM Driver INNER JOIN Job ON Driver.DriverID = Job.DriverID INNER JOIN DispatchGroup ON Driver.DispatchGroupID = DispatchGroup.DispatchGroupID INNER JOIN JobStop ON Job.JobID = JobStop.JobIDWHERE JobStop.ArriveDateTime BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0) AND DATEADD(DAY, - 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) + 1, 0))) tWHERE t .FSeq = 1 OR t .BSeq = 1ORDER BY t .DriverCode, t .ArriveDateTime Were getting closer, but I'm not sure that this query is doing exactly what I want it to do... as I'm now noticing, not all drivers have a date in "JobStop.ArriveDateTime" sometimes it's "NULL", if it is "NULL" the date lies in the "JobStop.PodDateTime" column. Could this be messing with the query?Currently, the results look like this:FSeq: "1" -- BSeq: "232" -- DriverCode: "994" -- ArriveDateTime: "9/2/2008 7:47:00 AM"FSeq: "232" -- BSeq: "1" -- DriverCode: "994" -- ArriveDateTime: "9/25/2008 8:34:00 AM" So on and so forth... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-25 : 12:30:50
|
may be thisSELECT *FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Driver.DriverCode,DATEADD(dd,DATEDIFF(dd,0,COALESCE(JobStop.ArriveDateTime,JobStop.PodDateTime)),0) ORDER BY COALESCE(JobStop.ArriveDateTime,JobStop.PodDateTime)) AS FSeq, ROW_NUMBER() OVER (PARTITION BY Driver.DriverCode,DATEADD(dd,DATEDIFF(dd,0,COALESCE(JobStop.ArriveDateTime,JobStop.PodDateTime)),0)ORDER BY COALESCE(JobStop.ArriveDateTime,JobStop.PodDateTime) DESC) AS BSeq, Driver.DriverCode, JobStop.ArriveDateTime,JobStop.PodDateTimeFROM Driver INNER JOIN Job ON Driver.DriverID = Job.DriverID INNER JOIN DispatchGroup ON Driver.DispatchGroupID = DispatchGroup.DispatchGroupID INNER JOIN JobStop ON Job.JobID = JobStop.JobIDWHERE JobStop.ArriveDateTime BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0) AND DATEADD(DAY, - 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) + 1, 0))) tWHERE t .FSeq = 1 OR t .BSeq = 1ORDER BY t .DriverCode, t .ArriveDateTime |
 |
|
|
knox203
Starting Member
31 Posts |
Posted - 2008-09-25 : 12:33:45
|
quote: Originally posted by visakh16 may be thisSELECT *FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Driver.DriverCode,DATEADD(dd,DATEDIFF(dd,0,JobStop.ArriveDateTime),0) ORDER BY JobStop.ArriveDateTime) AS FSeq, ROW_NUMBER() OVER (PARTITION BY Driver.DriverCode,DATEADD(dd,DATEDIFF(dd,0,JobStop.ArriveDateTime),0)ORDER BY JobStop.ArriveDateTime DESC) AS BSeq, Driver.DriverCode, JobStop.ArriveDateTimeFROM Driver INNER JOIN Job ON Driver.DriverID = Job.DriverID INNER JOIN DispatchGroup ON Driver.DispatchGroupID = DispatchGroup.DispatchGroupID INNER JOIN JobStop ON Job.JobID = JobStop.JobIDWHERE JobStop.ArriveDateTime BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0) AND DATEADD(DAY, - 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) + 1, 0))) tWHERE t .FSeq = 1 OR t .BSeq = 1ORDER BY t .DriverCode, t .ArriveDateTime
That right there is a thing a beauty, works like a charm! Thanks so much visakh16, also thanks to everyone else for responding!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-25 : 13:16:46
|
quote: Originally posted by knox203
quote: Originally posted by visakh16 may be thisSELECT *FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Driver.DriverCode,DATEADD(dd,DATEDIFF(dd,0,JobStop.ArriveDateTime),0) ORDER BY JobStop.ArriveDateTime) AS FSeq, ROW_NUMBER() OVER (PARTITION BY Driver.DriverCode,DATEADD(dd,DATEDIFF(dd,0,JobStop.ArriveDateTime),0)ORDER BY JobStop.ArriveDateTime DESC) AS BSeq, Driver.DriverCode, JobStop.ArriveDateTimeFROM Driver INNER JOIN Job ON Driver.DriverID = Job.DriverID INNER JOIN DispatchGroup ON Driver.DispatchGroupID = DispatchGroup.DispatchGroupID INNER JOIN JobStop ON Job.JobID = JobStop.JobIDWHERE JobStop.ArriveDateTime BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0) AND DATEADD(DAY, - 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) + 1, 0))) tWHERE t .FSeq = 1 OR t .BSeq = 1ORDER BY t .DriverCode, t .ArriveDateTime
That right there is a thing a beauty, works like a charm! Thanks so much visakh16, also thanks to everyone else for responding!!
welcome Buddy |
 |
|
|
|
|
|
|
|