question

AddiKhan avatar image
AddiKhan asked

How do I add CASE in my query in SQL Server 2008 and 2012?

I want to add case please help vehicle = 'Stop' addvalues start from starting, and it movingstatus = 'carmoving' values add contentiously in addvalues column insert TackHistory values(GETDATE(),GETDATE(),'Moving') WITH rows AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY gps_time) AS rn FROM TackHistory WHERE car_id = 12956 ) , differences AS ( SELECT mc.rn, mc.gps_time, DATEDIFF(second, mc.gps_time, mp.gps_time) time_diff FROM rows mc JOIN rows mp ON mc.rn = mp.rn - 1 ) SELECT t1.gps_time, t1.time_diff, SUM(t2.time_diff) time_sum FROM differences t1 JOIN differences t2 ON t1.rn >= t2.rn GROUP BY t1.rn, t1.gps_time, t1.time_diff ORDER BY t1.rn SQL Server 2012 query: SELECT gps_time, DATEDIFF(second, LAG(gps_time) OVER (ORDER BY gps_time), gps_time) as time_diff, DATEDIFF(second, MIN(gps_time) OVER (ORDER BY gps_time), gps_time) as time_sum FROM TackHistory ORDER BY gps_time ![alt text][1] ![alt text][2] [1]: /storage/temp/3401-gflcq.jpg [2]: /storage/temp/3402-hmq6b.jpg
sql-server-2008sql-serversql server 2012
gflcq.jpg (21.4 KiB)
hmq6b.jpg (46.2 KiB)
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

seanlange avatar image seanlange commented ·
0 Likes 0 ·

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.