将开始和结束列合并为一列

时间:2016-11-29 13:19:23

标签: sql-server tsql sql-server-2014

我已经搜索了好几个星期,现在试图找到解决问题的方法。

我的问题如下:

A有一张表,上面有车辆遥测提供者的起点和终点坐标和日期。

我需要将这些合并到一个列中,以便我们的报告解决方案能够绘制它们。

一些示例数据如下:

s <- function(...){
   sum(..., na.rm = TRUE)
}

看起来像这样:

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)

我想要的输出如下:

+-----------+---------------------+---------------------------------+------------+------------+---------------------+---------------------------------+------------+------------+
| 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 |
+-----------+---------------------+---------------------------------+------------+------------+---------------------+---------------------------------+------------+------------+

任何想法/代码都将受到极大的欢迎。

谢谢,

3 个答案:

答案 0 :(得分:5)

通过使用以下内容,您可以获得所需的结果。您可以使用Cross apply with values子句。

     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) 

答案 1 :(得分:1)

Query使用如下所示的联合

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

答案 2 :(得分:0)

我认为这就是你所追求的:

SELECT 
ServiceID, 
DENSE_RANK() OVER(
             PARTITION BY ServiceID 
             ORDER BY CASE WHEN CJ.R=1 THEN [StartDate] ELSE EndDate END) PathOrder,
CASE WHEN CJ.R=1 THEN [StartDate] ELSE EndDate END [Date],
CASE WHEN CJ.R=1 THEN [StartLocation] ELSE [EndLocation] END [Location],
CASE WHEN CJ.R=1 THEN [StartLat] ELSE [EndLat] END [Lat],
CASE WHEN CJ.R=1 THEN [StartLong] ELSE [EndLong] END [Long]
FROM @TblVar 
CROSS JOIN 
(SELECT 1 As R UNION ALL SELECT 2) CJ