Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Start And End Columns To One Column

I have searched high and low for weeks now trying to find a solution to my problem.

My problem is as follows:

A have a table with start and end co-ordinates and dates from a vehicle telemetry provider.

I need to merge these into one column in order for our reporting solution to be able to plot them.

Some example data is as follows:

DECLARE @TblVar AS TABLE([ServiceID] INT
                    ,[StartDate] DATETIME
                    ,[StartLocation] VARCHAR(255)
                    ,[StartLat] FLOAT
                    ,[StartLong] FLOAT
                    ,[EndDate] DATETIME
                    ,[EndLocation] VARCHAR(255)
                    ,[EndLat] FLOAT
                    ,[EndLong] FLOAT);
INSERT INTO @TblVar

VALUES (48322, '2016-11-28 05:38:37.000','Weaver Road,Leicester',52.5753273,-1.306842,'2016-11-28 05:40:02.000','Earl Street,Leicester',52.5781,-1.3048711)
      ,(48322, '2016-11-28 05:44:05.000','Earl Street,Leicester',52.5780996,-1.3048768,'2016-11-28 05:53:52.000','Arbor Road,Leicester',52.5533448,-1.2349645)
      ,(48322, '2016-11-28 09:14:25.000','Arbor Road,Leicester',52.5533833,-1.2349445,'2016-11-28 09:15:54.000','Coventry Road,Leicester',52.5522865,-1.2370495)  
      ,(48322, '2016-11-28 09:16:05.000','Coventry Road,Leicester',52.552292,-1.2370598,'2016-11-28 09:20:41.000','Arbor Road,Leicester',52.553338,-1.2352033)    
      ,(48322, '2016-11-28 13:34:36.000','Arbor Road,Leicester',52.553388,-1.2352651,'2016-11-28 13:53:29.000','Narborough Road South,Leicester',52.597422,-1.1832976)      
      ,(48322, '2016-11-28 13:55:36.000','Narborough Road South,Leicester',52.597352,-1.183299,'2016-11-28 13:58:51.000','Hazel Drive,Leicester',52.6020946,-1.1760238)
      ,(48322, '2016-11-28 14:11:53.000','Hazel Drive,Leicester',52.6020853,-1.1760053,'2016-11-28 14:24:40.000','Arbor Road,Leicester',52.5533118,-1.2352118)
      ,(48322, '2016-11-28 14:29:48.000','Arbor Road,Leicester',52.5532741,-1.2352471,'2016-11-28 14:30:39.000','Coventry Road,Leicester',52.552955,-1.2363475)
      ,(48322, '2016-11-28 14:32:24.000','Coventry Road,Leicester',52.552944,-1.2363491,'2016-11-28 14:34:06.000','Coventry Road,Leicester',52.5532598,-1.2350731) 
      ,(48322, '2016-11-28 14:50:12.000','Coventry Road,Leicester',52.5532646,-1.2351661,'2016-11-28 14:50:13.000','Coventry Road,Leicester',52.5532646,-1.2351661)  
      ,(48322, '2016-11-28 16:02:29.000','Coventry Road,Leicester',52.553156,-1.2348643,'2016-11-28 16:13:09.000','Earl Street,Leicester',52.5780946,-1.30486)
      ,(48322, '2016-11-28 16:22:07.000','Earl Street,Leicester',52.5780776,-1.304851,'2016-11-28 16:23:26.000','Weaver Road,Leicester',52.5753643,-1.306814)

Which looks like this:

+-----------+---------------------+---------------------------------+------------+------------+---------------------+---------------------------------+------------+------------+
| ServiceID |      StartDate      |          StartLocation          |  StartLat  | StartLong  |       EndDate       |           EndLocation           |   EndLat   |  EndLong   |
+-----------+---------------------+---------------------------------+------------+------------+---------------------+---------------------------------+------------+------------+
|     48322 | 28/11/2016 05:38:37 | Weaver Road,Leicester           | 52.5753273 |  -1.306842 | 28/11/2016 05:40:02 | Earl Street,Leicester           |    52.5781 | -1.3048711 |
|     48322 | 28/11/2016 05:44:05 | Earl Street,Leicester           | 52.5780996 | -1.3048768 | 28/11/2016 05:53:52 | Arbor Road,Leicester            | 52.5533448 | -1.2349645 |
|     48322 | 28/11/2016 09:14:25 | Arbor Road,Leicester            | 52.5533833 | -1.2349445 | 28/11/2016 09:15:54 | Coventry Road,Leicester         | 52.5522865 | -1.2370495 |
|     48322 | 28/11/2016 09:16:05 | Coventry Road,Leicester         |  52.552292 | -1.2370598 | 28/11/2016 09:20:41 | Arbor Road,Leicester            |  52.553338 | -1.2352033 |
|     48322 | 28/11/2016 13:34:36 | Arbor Road,Leicester            |  52.553388 | -1.2352651 | 28/11/2016 13:53:29 | Narborough Road South,Leicester |  52.597422 | -1.1832976 |
|     48322 | 28/11/2016 13:55:36 | Narborough Road South,Leicester |  52.597352 |  -1.183299 | 28/11/2016 13:58:51 | Hazel Drive,Leicester           | 52.6020946 | -1.1760238 |
|     48322 | 28/11/2016 14:11:53 | Hazel Drive,Leicester           | 52.6020853 | -1.1760053 | 28/11/2016 14:24:40 | Arbor Road,Leicester            | 52.5533118 | -1.2352118 |
|     48322 | 28/11/2016 14:29:48 | Arbor Road,Leicester            | 52.5532741 | -1.2352471 | 28/11/2016 14:30:39 | Coventry Road,Leicester         |  52.552955 | -1.2363475 |
|     48322 | 28/11/2016 14:32:24 | Coventry Road,Leicester         |  52.552944 | -1.2363491 | 28/11/2016 14:34:06 | Coventry Road,Leicester         | 52.5532598 | -1.2350731 |
|     48322 | 28/11/2016 14:50:12 | Coventry Road,Leicester         | 52.5532646 | -1.2351661 | 28/11/2016 14:50:13 | Coventry Road,Leicester         | 52.5532646 | -1.2351661 |
|     48322 | 28/11/2016 16:02:29 | Coventry Road,Leicester         |  52.553156 | -1.2348643 | 28/11/2016 16:13:09 | Earl Street,Leicester           | 52.5780946 |   -1.30486 |
|     48322 | 28/11/2016 16:22:07 | Earl Street,Leicester           | 52.5780776 |  -1.304851 | 28/11/2016 16:23:26 | Weaver Road,Leicester           | 52.5753643 |  -1.306814 |
+-----------+---------------------+---------------------------------+------------+------------+---------------------+---------------------------------+------------+------------+

My desired output is as follows:

+-----------+-----------+---------------------+---------------------------------+------------+------------+
| ServiceID | PathOrder |        Date         |            Location             | Lattitude  | Longitude  |
+-----------+-----------+---------------------+---------------------------------+------------+------------+
|     48322 |         1 | 28/11/2016 05:38:37 | Weaver Road,Leicester           | 52.5753273 |  -1.306842 |
|     48322 |         2 | 28/11/2016 05:40:02 | Earl Street,Leicester           |    52.5781 | -1.3048711 |
|     48322 |         3 | 28/11/2016 05:44:05 | Earl Street,Leicester           | 52.5780996 | -1.3048768 |
|     48322 |         4 | 28/11/2016 05:53:52 | Arbor Road,Leicester            | 52.5533448 | -1.2349645 |
|     48322 |         5 | 28/11/2016 09:14:25 | Arbor Road,Leicester            | 52.5533833 | -1.2349445 |
|     48322 |         6 | 28/11/2016 09:15:54 | Coventry Road,Leicester         | 52.5522865 | -1.2370495 |
|     48322 |         7 | 28/11/2016 09:16:05 | Coventry Road,Leicester         |  52.552292 | -1.2370598 |
|     48322 |         8 | 28/11/2016 09:20:41 | Arbor Road,Leicester            |  52.553338 | -1.2352033 |
|     48322 |         9 | 28/11/2016 13:34:36 | Arbor Road,Leicester            |  52.553388 | -1.2352651 |
|     48322 |        10 | 28/11/2016 13:53:29 | Narborough Road South,Leicester |  52.597422 | -1.1832976 |
|     48322 |        11 | 28/11/2016 13:55:36 | Narborough Road South,Leicester |  52.597352 |  -1.183299 |
|     48322 |        12 | 28/11/2016 13:58:51 | Hazel Drive,Leicester           | 52.6020946 | -1.1760238 |
|     48322 |        13 | 28/11/2016 14:11:53 | Hazel Drive,Leicester           | 52.6020853 | -1.1760053 |
|     48322 |        14 | 28/11/2016 14:24:40 | Arbor Road,Leicester            | 52.5533118 | -1.2352118 |
|     48322 |        15 | 28/11/2016 14:29:48 | Arbor Road,Leicester            | 52.5532741 | -1.2352471 |
|     48322 |        16 | 28/11/2016 14:30:39 | Coventry Road,Leicester         |  52.552955 | -1.2363475 |
|     48322 |        17 | 28/11/2016 14:32:24 | Coventry Road,Leicester         |  52.552944 | -1.2363491 |
|     48322 |        18 | 28/11/2016 14:34:06 | Coventry Road,Leicester         | 52.5532598 | -1.2350731 |
|     48322 |        19 | 28/11/2016 14:50:12 | Coventry Road,Leicester         | 52.5532646 | -1.2351661 |
|     48322 |        20 | 28/11/2016 14:50:13 | Coventry Road,Leicester         | 52.5532646 | -1.2351661 |
|     48322 |        21 | 28/11/2016 16:02:29 | Coventry Road,Leicester         |  52.553156 | -1.2348643 |
|     48322 |        22 | 28/11/2016 16:13:09 | Earl Street,Leicester           | 52.5780946 |   -1.30486 |
|     48322 |        23 | 28/11/2016 16:22:07 | Earl Street,Leicester           | 52.5780776 |  -1.304851 |
|     48322 |        24 | 28/11/2016 16:23:26 | Weaver Road,Leicester           | 52.5753643 |  -1.306814 |
+-----------+-----------+---------------------+---------------------------------+------------+------------+

Any ideas / code will be greatly received.

Thanks,

Dan

like image 279
Daniel Hitchcock Avatar asked Nov 29 '16 13:11

Daniel Hitchcock


People also ask

How do I combine multiple columns into one column in pandas?

You can use DataFrame. apply() for concatenate multiple column values into a single column, with slightly less typing and more scalable when you want to join multiple columns .


2 Answers

By using below you can get the desired results. You can use Cross apply with values clause.

     SELECT A.SERVICEID,
       ROW_NUMBER() OVER(PARTITION BY A.SERVICEID ORDER BY A.SERVICEID) AS  PATHORDER,
       B.*
FROM   @TBLVAR A
       CROSS APPLY(
       VALUES
              ([STARTDATE],[STARTLOCATION],[STARTLAT],[STARTLONG]),
              ([ENDDATE],[ENDLOCATION],[ENDLAT],[ENDLONG])
              )B(DATE, LOCATION, LATTITUDE, LONGITUD) 
like image 85
Tharunkumar Reddy Avatar answered Nov 15 '22 04:11

Tharunkumar Reddy


the Query uses union like below

SELECT ServiceID, ROW_NUMBER () over ( order by StartDate) AS PathOrder, StartDate AS Date, StartLocation AS Location, StartLat AS Lattitude, StartLong AS Longitude
FROM (SELECT ServiceID, StartDate, StartLocation, StartLat, StartLong
 FROM @TblVar
 UNION ALL
 SELECT ServiceID, EndDate, EndLocation, EndLat, EndLong
 FROM TblVar AS @TblVar_1) AS q1
like image 27
Bassam Ruahimah Avatar answered Nov 15 '22 05:11

Bassam Ruahimah