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
 General SQL Server Forums
 New to SQL Server Programming
 How to write query to select second row column dat

Author  Topic 

Hari k
Starting Member

5 Posts

Posted - 2013-11-18 : 00:49:32
Hi I have a requirement like below can any one help for me.
input table will be like below:
eventdata:
accountID deviceID timestamp speedKPH address
--------- -------- ---------- -------- -------------
preva1 bolero 1359089006 15 Ullalu Road
preva1 bolero 1359088796 0 Ullalu Road
preva1 bolero 1359088886 0 Ullalu Road
preva1 bolero 1359088888 8.47 Ullalu Road
preva1 bolero 1359088986 0 Ullalu Road
preva1 bolero 1359088988 45 Ullalu Road
preva1 bolero 1359088996 21 Ullalu Road
preva1 bolero 1359088998 0 Ullalu Road
preva1 bolero 1359089006 15 Ullalu Road
preva1 bolero 1359089009 12 Ullalu Road
preva1 bolero 1359089006 15 Ullalu Road
preva1 bolero 1359089016 0 Ullalu Road
preva1 bolero 1359089026 0 Ullalu Road
So here i need output table like below:
stoppagedetails:
accountID deviceID from_timestamp to_timestamp diff
--------- -------- ---------- -------- -------------
preva1 bolero 1359088796 1359088888 92
preva1 bolero 1359088986 1359088988 2
preva1 bolero 1359088998 1359089006 8
preva1 bolero 1359089016

So can any one help how to write mysql query for the above reqirement.Thanks in advance..

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-18 : 05:39:36
Something like this if in sql server (dont know about mysql)


;With CTE
AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY accountID, deviceID ORDER BY timestamp) AS Seq,*
FROM YourTable
)

SELECT t.accountID, t.deviceID,
t.[timestamp] AS from_timestamp,
t1.nexttimestamp AS to_timestamp
t1.nexttimestamp - t.[timestamp] AS diff
FROM CTE t
OUTER APPLY (SELECT MIN([timestamp]) AS nexttimestamp
FROM CTE
WHERE accountID = t.accountID
AND deviceID = t.deviceID
AND Seq > t.Seq
AND speedKPH <> 0
)t1
WHERE t.speedKPH = 0
AND NOT EXISTS (SELECT 1
FROM CTE
WHERE accountID = t.accountID
AND deviceID = t.deviceID
AND Seq = t.Seq -1
AND speedKPH = 0)

Try this and if it doesnt work please post in some mysql forums for more help.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Hari k
Starting Member

5 Posts

Posted - 2013-11-19 : 03:38:27
thanks for reply i understood to some extent but i didn't understood what "with CTE" means in mysql
So can any one help for this

quote:
Originally posted by visakh16

Something like this if in sql server (dont know about mysql)


;With CTE
AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY accountID, deviceID ORDER BY timestamp) AS Seq,*
FROM YourTable
)

SELECT t.accountID, t.deviceID,
t.[timestamp] AS from_timestamp,
t1.nexttimestamp AS to_timestamp
t1.nexttimestamp - t.[timestamp] AS diff
FROM CTE t
OUTER APPLY (SELECT MIN([timestamp]) AS nexttimestamp
FROM CTE
WHERE accountID = t.accountID
AND deviceID = t.deviceID
AND Seq > t.Seq
AND speedKPH <> 0
)t1
WHERE t.speedKPH = 0
AND NOT EXISTS (SELECT 1
FROM CTE
WHERE accountID = t.accountID
AND deviceID = t.deviceID
AND Seq = t.Seq -1
AND speedKPH = 0)

Try this and if it doesnt work please post in some mysql forums for more help.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-11-19 : 04:26:53
SQLTeam.com is on Microsoft SQL Server. As you are using mysql, you will get better reply at a mysql forum like dbforums.com


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -