Sunday 15 September 2013

SQL Server - Group by few columns and put in one row in appropriate columns -


I need to work but do not imagine implementing the table with the following columns in the SQL Server database :

  id start timeout ostate oind 1 111 222 01-01-2013 111 555 1 222 333 02-01-2013 111 555 1 333 444 03-01-2013 111 555 1 444 555 04-01-2013 111 555 1 444 555 01-01-2013 444 666 1 555 666 02-01-2013 444 666   

I want to take an intermediate point from start to finish, Unless the end is equal to one and one row The cast should be 2 rows results table for this example: intermediate points 222-333-444 and 444-666 Ostart-Ond with Intrmidiatt point 555111-555 for Ostrt-Oand. The maximum number of intermediate points is 5. Therefore the result table will like: ID OSTAR O & P time 1 time 1 point 2 time 2 point 3 point 4 point 5 1 111 555 01-01-2013 222 02-01-2013 333 03-01-2013 444 1 444 666 01-01-2013 555 02-01-2013 How can I get such results table?

You can use the PIVOT function to get results in many columns with your multiple row values . If you apply the row_number () windowing function, you can get a point value of 1-5 points: ID, Ostert, Oend, Point 1, Point 2, Point 3, Point 4, Vouchar as Point 5 (Selection ID, [End], Ostert, Oand, 'Point' + Cast (row_number ()) (vartar (10)) (10) as your Seek from System (Issue 1, point 2, Point 3, Point 4, Point 5), see ([end]) PIV;

.

If you have two columns on Pivot If you want, you will need the inconvenience and then smoke the data:

  id, ostart, oynd, point 1, time 1, point 2, time 2, point 3, time 3, From Point 4, Time 4, Point 5, Time 5 (Selective ID, Ostert, Ond, COL + Cast (Secret Vicar (10)) Call, Select Value, Varchar (10)) as (seq from yourtable) Form (selection id, [end], time, ostart, oynd, cast (line_number (), (id by initialization, split by split) src cro Select the 's' application ('time') Convert (worker (10), time, 120) Union Select all 'Point', varchar (10)) as cast ((end)) C (call, value)) Pivot (To make a maximum value (point 1, time 1, point 2, time 2, point 3, time 3, point 4, time 4, point 5, time 5)) Piv;   

View

No comments:

Post a Comment