sql - convert row to column using Pivot without any clause -


i have table below.

enter image description here

i need data below.

enter image description here

i have created 2 temp tables , achieved result this. please me same pivot.

at least wouldn't use pivot that, mind simpler group , row_number:

select userid, max(starttime) starttime, max(endtime) endtime (   select userid,     case when startorend = 's' time end starttime,     case when startorend = 'e' time end endtime,     row_number() on (partition userid order time asc)        + case when startorend = 's' 1 else 0 end grp   table1 ) x group userid, grp order starttime 

the derived table splits time start / end time columns (to handle cases 1 exists) , uses trick row number group s / e items together. outer select groups rows same row.

example in sql fiddle


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -