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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Assign a max time if time differs by 10 secs

Author  Topic 

shilpagupta4
Starting Member

3 Posts

Posted - 2010-04-01 : 16:16:40
User_Id Time_stamp
2454558665777080000 1/8/2010 16:19:56
2454558665777080000 1/8/2010 16:19:57
2454558665777080000 1/8/2010 16:19:38
2454558665777080000 1/8/2010 16:19:38
2481340376380600000 1/12/2010 10:53:52
2481340376380600000 1/12/2010 10:53:53
2481340376380600000 1/12/2010 10:54:47
2481340376380600000 1/12/2010 10:54:47
2481340376380600000 1/12/2010 10:54:47
2481340376380600000 1/12/2010 10:54:47
I want to assign max time stamp if time_stamp differs by 10 secs and group by user_id? How do we do it in SQL?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-04-01 : 16:18:43
what is your expected output?
Go to Top of Page

shilpagupta4
Starting Member

3 Posts

Posted - 2010-04-01 : 16:29:07
User_id Time_stamp Common_time
2454558665777080000 1/8/2010 16:19:56 1/8/2010 16:19:57
2454558665777080000 1/8/2010 16:19:57 1/9/2010 16:19:57
2454558665777080000 1/8/2010 16:19:38 1/8/2010 16:19:38
2454558665777080000 1/8/2010 16:19:38 1/8/2010 16:19:38
2481340376380600000 1/12/2010 10:53:52 1/12/2010 10:53:53
2481340376380600000 1/12/2010 10:53:53 1/13/2010 10:53:53
2481340376380600000 1/12/2010 10:54:47 1/12/2010 10:54:47
2481340376380600000 1/12/2010 10:54:47 1/12/2010 10:54:47
2481340376380600000 1/12/2010 10:54:47 1/12/2010 10:54:47
2481340376380600000 1/12/2010 10:54:47 1/12/2010 10:54:47

Above is my expected output.
You can use this function for second time difference (abs(datediff(ss,a.time_stamp,b.time_stamp))<=10)but, how to get this output.I have millions of such rows by user_id.

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-04-01 : 16:48:19
I'm not sure I understand how youa re grouping the rows into groups of two, but maybe this works?
DECLARE @Foo TABLE (User_ID BIGINT, Time_stamp DATETIME)

INSERT @Foo
SELECT 2454558665777080000, '1/8/2010 16:19:56'
UNION ALL SELECT 2454558665777080000, '1/8/2010 16:19:57'
UNION ALL SELECT 2454558665777080000, '1/8/2010 16:19:38'
UNION ALL SELECT 2454558665777080000, '1/8/2010 16:19:38'
UNION ALL SELECT 2481340376380600000, '1/12/2010 10:53:52'
UNION ALL SELECT 2481340376380600000, '1/12/2010 10:53:53'
UNION ALL SELECT 2481340376380600000, '1/12/2010 10:54:47'
UNION ALL SELECT 2481340376380600000, '1/12/2010 10:54:47'
UNION ALL SELECT 2481340376380600000, '1/12/2010 10:54:47'
UNION ALL SELECT 2481340376380600000, '1/12/2010 10:54:47'


SELECT
A.User_ID,
CASE
WHEN A.RowNum % 2 = 0
THEN A.Time_stamp
ELSE
CASE
WHEN DATEDIFF(SECOND, A.Time_stamp, B.Time_stamp) >= 10
THEN B.Time_stamp
ELSE A.Time_Stamp
END
END AS Time_stamp
FROM
(
SELECT User_ID, Time_Stamp, ROW_NUMBER() OVER (PARTITION BY User_ID ORDER BY Time_Stamp) AS RowNum
FROM @Foo
) AS A
LEFT OUTER JOIN
(
SELECT User_ID, Time_Stamp, ROW_NUMBER() OVER (PARTITION BY User_ID ORDER BY Time_Stamp) AS RowNum
FROM @Foo
) AS B
ON
A.User_ID = B.User_ID
AND A.RowNum = B.RowNum - 1
Go to Top of Page
   

- Advertisement -