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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-08-26 : 11:13:41
|
alex writes "I am trying to perform a query on a table that has the following columns:Row_IDDateTimeStampSomeValueThe end user would like to be able to query for the table for records that have an DateTimeStamp interval that is 5 minutes apart. The problem I am having is that When I do a DATEDIFF in the query against the minites in the the DateTimeStamp column, multiple records are returned. Here is the query I have been using to get to the point I am at now:SELECT DATEDIFF(n, '7/9/2002 10:24:44', DateTimeStamp) AS minDiff, Row_ID,SomeValueFROM MM_SIP64F_RunDetailsWHERE (DATEDIFF(n, '7/9/2002 10:24:44', DateTimeStamp) % 5 = 0)'7/9/2002 10:24:44' is just an arbitrary date that I am usingThe query current returns something like this:0 7760 2002-07-09 10:24:44.000 1.10 7761 2002-07-09 10:24:44.000 2.20 7762 2002-07-09 10:24:54.000 3.25 7788 2002-07-09 10:29:04.000 4.25 7789 2002-07-09 10:29:14.000 5.25 7793 2002-07-09 10:29:54.000 6.210 7820 2002-07-09 10:34:04.000 7.210 7821 2002-07-09 10:34:14.000 9.210 7822 2002-07-09 10:34:24.000 9.215 7852 2002-07-09 10:39:04.000 10.215 7856 2002-07-09 10:39:44.000 11.215 7857 2002-07-09 10:39:54.000 12.2 I would like it to return the first occurance of the matching interval like this:0 7760 2002-07-09 10:24:44.000 1.15 7788 2002-07-09 10:29:04.000 4.210 7820 2002-07-09 10:34:04.000 7.215 7852 2002-07-09 10:39:04.000 10.2 " |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2002-08-26 : 21:28:43
|
| Try mixing in min(Row_ID) and GROUP BY. You might need to do a correlated subquery in the WHERE clause to restrict the rows. |
 |
|
|
alexc
Starting Member
10 Posts |
Posted - 2002-08-27 : 10:31:23
|
I have tried min(Row_ID) and Group By without any luck. I have tried several different subqueries and still have nto found one that works. I hope someone comes along that can solve this problem.Alexquote: Try mixing in min(Row_ID) and GROUP BY. You might need to do a correlated subquery in the WHERE clause to restrict the rows.
|
 |
|
|
Onamuji
Aged Yak Warrior
504 Posts |
Posted - 2002-08-27 : 10:42:08
|
| you almost had it :)DECLARE @t TABLE (Row_ID INT IDENTITY, DateTimeStamp DATETIME, SomeValue DECIMAL(38, 8))DECLARE @i INTSET @i = 0WHILE @i < 12 BEGIN INSERT INTO @t (DateTimeStamp, SomeValue) VALUES (DATEADD(n, (@i % 5) * 5, '2002-07-09 10:24:44.000'), @i * RAND() + 1) SET @i = @i + 1 ENDSELECT minRows.minDiff, t.Row_ID, t.SomeValue FROM @t AS t INNER JOIN (SELECT DATEDIFF(n, '7/9/2002 10:24:44', DateTimeStamp) AS minDiff, MIN(Row_ID) AS Row_ID FROM @t WHERE (DATEDIFF(n, '7/9/2002 10:24:44', DateTimeStamp) % 5 = 0) GROUP BY DATEDIFF(n, '7/9/2002 10:24:44', DateTimeStamp)) minRows ON t.Row_ID = minRows.Row_ID |
 |
|
|
alexc
Starting Member
10 Posts |
Posted - 2002-08-27 : 10:49:02
|
Thanks,Thats exactly what I needed.Alexquote: you almost had it :)DECLARE @t TABLE (Row_ID INT IDENTITY, DateTimeStamp DATETIME, SomeValue DECIMAL(38, 8))DECLARE @i INTSET @i = 0WHILE @i < 12 BEGIN INSERT INTO @t (DateTimeStamp, SomeValue) VALUES (DATEADD(n, (@i % 5) * 5, '2002-07-09 10:24:44.000'), @i * RAND() + 1) SET @i = @i + 1 ENDSELECT minRows.minDiff, t.Row_ID, t.SomeValue FROM @t AS t INNER JOIN (SELECT DATEDIFF(n, '7/9/2002 10:24:44', DateTimeStamp) AS minDiff, MIN(Row_ID) AS Row_ID FROM @t WHERE (DATEDIFF(n, '7/9/2002 10:24:44', DateTimeStamp) % 5 = 0) GROUP BY DATEDIFF(n, '7/9/2002 10:24:44', DateTimeStamp)) minRows ON t.Row_ID = minRows.Row_ID
|
 |
|
|
|
|
|
|
|