| Author |
Topic |
|
naidu4u
Starting Member
23 Posts |
Posted - 2010-10-21 : 16:55:17
|
| Hello,I have three columns in my tableeventid comment timestamp1 fiu 2010-10-12 12:25:002 abc 2010-10-13 03:19:002 xyz 2010-10-13 03:20:002 123 2010-10-13 03:29:003 kri 2010-10-14 20:29:00and I am trying to get the time difference between the rows with same eventidThanks in advance. |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-10-21 : 17:18:03
|
| [code]declare @t table (eventid int, comment char(3), timestamp datetime)insert @t select1, 'fiu', '2010-10-12 12:25:00' union all select 2, 'abc', '2010-10-13 03:19:00' union all select 2, 'xyz', '2010-10-13 03:20:00' union all select 2, '123', '2010-10-13 03:29:00' union all select 3, 'kri', '2010-10-14 20:29:00';with t(eventid, comment, timestamp, rn) as (select *, row_number() over (partition by eventid order by timestamp) from @t)select a.*, datediff(s, b.timestamp, a.timestamp) from t aleft join t b on a.eventid=b.eventid and a.rn=b.rn+1[/code]Change table name accordingly. |
 |
|
|
naidu4u
Starting Member
23 Posts |
Posted - 2010-10-21 : 17:45:23
|
| I am trying to understand this but my actual prob is that I have two tables(X,Y) in whichX:'eventID' 'Station' 'StartTime'with 'z' no .of rowsY:'ID' 'refEventID' 'Comment' 'TimeStamp'with 's' no .of rowsI need to find the time difference between each comment with the same eventID (refEventID) per station and StartTimeso what I have done is thatselect a.eventid, a.stn, substring(convert(varchar(8),a.localstarttime,112),1,4)+'/'+substring(convert(varchar(8),a.localstarttime,112),5,2)+'/'+substring(convert(varchar(8),a.localstarttime,112),7,2) xyz, b.refeventid, b.comment, b."timestamp" from X a join Y b on a.eventid = b.refeventidgroup by a.eventid, a.stn, a.localstarttime, b.refeventid, b.comment, b."timestamp"so after writing this query I got the result like(eventid) (comment) (timestamp) 1 fiu 2010-10-12 12:25:00 2 abc 2010-10-13 03:19:00 2 xyz 2010-10-13 03:20:00 2 123 2010-10-13 03:29:00 3 kri 2010-10-14 20:29:00 with 'q' no. of rows and now I want to get the time difference between the timestamps with same eventID. |
 |
|
|
naidu4u
Starting Member
23 Posts |
Posted - 2010-10-22 : 10:48:15
|
| Thanks a lot RobvolkJust now implemented the query but I am using SQL Server 2000 which is not allowing me to use row_number() as a function so I tried to write something inorder to generate the rownumber and am getting an error msg when have written this queryselect k.* from(select a.eventid,substring(convert(varchar(8),a.localstarttime,112),1,4)+'/'+substring(convert(varchar(8),a.localstarttime,112),5,2)+'/'+substring(convert(varchar(8),a.localstarttime,112),7,2) xyz,b.refeventid, b.comment, b."timestamp"from a join bon a.eventid = b.refeventidgroup by a.eventid, a.localstarttime, b.refeventid, b.comment, b."timestamp") k;with k as (select * from (select ( select count(*) from k as e2 where e2.timestamp <= e1.timestamp) as rn, e1.* from k as e1) h)select i1.*, datediff(mm, i1."timestamp", i2."timestamp") from k i1left join k i2on i1.eventid = i2.eventid and i1.rn=i2.rn+1;error: incorrect syntax near the keyword 'with'.Can you please help me with that....Thanks in advance... |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-10-22 : 13:00:47
|
Try this:SELECT a.eventid, a.stn, CONVERT(VARCHAR(8),a.localstarttime,111) xyz, b.refeventid, b.comment, b."timestamp", DATEDIFF(second, (SELECT MAX("timestamp") FROM Y WHERE refeventid=b.refeventid AND "timestamp"<b."timestamp"), b."timestamp") secondsFROM X a JOIN Y b ON a.eventid = b.refeventidI'm not sure this is exactly what you need as I couldn't test it.For future reference, please post SQL Server 2000 questions in the SQL 2000 forums. And please post your actual table structures at the outset. It impolite and annoying to reply with "sorry, my real tables are like this..." after someone posts a solution based on the first structure(s). |
 |
|
|
naidu4u
Starting Member
23 Posts |
Posted - 2010-10-22 : 13:19:13
|
quote: Originally posted by robvolk Try this:SELECT a.eventid, a.stn, CONVERT(VARCHAR(8),a.localstarttime,111) xyz, b.refeventid, b.comment, b."timestamp", DATEDIFF(second, (SELECT MAX("timestamp") FROM Y WHERE refeventid=b.refeventid AND "timestamp"<b."timestamp"), b."timestamp") secondsFROM X a JOIN Y b ON a.eventid = b.refeventidI'm not sure this is exactly what you need as I couldn't test it.For future reference, please post SQL Server 2000 questions in the SQL 2000 forums. And please post your actual table structures at the outset. It impolite and annoying to reply with "sorry, my real tables are like this..." after someone posts a solution based on the first structure(s).
I am really sorry and I dint mean that...But this is what I am exactly looking for...Thanks a lot dude...I love the sql.Actually I am working on Cognos so I was weak in SQL but trying to learn some good points from seniors like you...Thanks a ton and I really mean it. |
 |
|
|
naidu4u
Starting Member
23 Posts |
Posted - 2010-10-22 : 16:37:30
|
| need to find the average frequency of the comments for each eventid I tried writing this query but its giving me some errors...Please help me with this...select (select avg("difference") from t where eventid=k.eventid and "timestamp"< k."timestamp") average from(SELECT a.eventid, CONVERT(VARCHAR(10),a.localstarttime,111) xyz, b.refeventid, b.comment, b."timestamp", DATEDIFF(second, (SELECT MAX("timestamp") FROM Y WHERE refeventid=b.refeventid AND "timestamp"<b."timestamp"), b."timestamp") "difference"FROM X a JOIN Y b ON a.eventid = b.refeventidgroup by a.eventid, a.localstarttime, b.refeventid, b.comment, b."timestamp" ) t join t k on t.eventid = k.eventidbut its giving me an error saying that Server: Msg 208, Level 16, State 1, Line 1Invalid object name 't'.My table t is 12827 2010/07/28 12827 waitin 2010-07-28 18:50:00 NULL12827 2010/07/28 12827 wait/> 2010-07-28 18:52:00 12012827 2010/07/28 12827 check 2010-07-28 19:28:00 216015167 2010/09/08 15167 test. 2010-09-09 18:35:00 NULL15923 2010/09/22 15923 testt 2010-09-23 05:21:00 NULL16812 2010/10/12 16812 wor.. 2010-10-13 03:19:00 NULL16812 2010/10/12 16812 fie... 2010-10-13 03:20:00 6016812 2010/10/12 16812 test.. 2010-10-13 03:29:00 540 |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-10-22 : 18:04:49
|
If you need the average for all comments:select avg("difference") average from(SELECT a.eventid, CONVERT(VARCHAR(10),a.localstarttime,111) xyz, b.refeventid, b.comment, b."timestamp", DATEDIFF(second, (SELECT MAX("timestamp") FROM Y WHERE refeventid=b.refeventid AND "timestamp"<b."timestamp"), b."timestamp") "difference"FROM X a JOIN Y b ON a.eventid = b.refeventidgroup by a.eventid, a.localstarttime, b.refeventid, b.comment, b."timestamp" ) tIf you need the average for each event:select t.eventid, avg("difference") average from(SELECT a.eventid, CONVERT(VARCHAR(10),a.localstarttime,111) xyz, b.refeventid, b.comment, b."timestamp", DATEDIFF(second, (SELECT MAX("timestamp") FROM Y WHERE refeventid=b.refeventid AND "timestamp"<b."timestamp"), b."timestamp") "difference"FROM X a JOIN Y b ON a.eventid = b.refeventidgroup by a.eventid, a.localstarttime, b.refeventid, b.comment, b."timestamp" ) tgroup by t.eventid |
 |
|
|
naidu4u
Starting Member
23 Posts |
Posted - 2010-10-25 : 10:55:33
|
quote: Originally posted by robvolk If you need the average for all comments:select avg("difference") average from(SELECT a.eventid, CONVERT(VARCHAR(10),a.localstarttime,111) xyz, b.refeventid, b.comment, b."timestamp", DATEDIFF(second, (SELECT MAX("timestamp") FROM Y WHERE refeventid=b.refeventid AND "timestamp"<b."timestamp"), b."timestamp") "difference"FROM X a JOIN Y b ON a.eventid = b.refeventidgroup by a.eventid, a.localstarttime, b.refeventid, b.comment, b."timestamp" ) tIf you need the average for each event:select t.eventid, avg("difference") average from(SELECT a.eventid, CONVERT(VARCHAR(10),a.localstarttime,111) xyz, b.refeventid, b.comment, b."timestamp", DATEDIFF(second, (SELECT MAX("timestamp") FROM Y WHERE refeventid=b.refeventid AND "timestamp"<b."timestamp"), b."timestamp") "difference"FROM X a JOIN Y b ON a.eventid = b.refeventidgroup by a.eventid, a.localstarttime, b.refeventid, b.comment, b."timestamp" ) tgroup by t.eventid
Thank you robvolk...you simply rock... |
 |
|
|
|
|
|