如何在 SQL Server 2014 中将一个字段的结果拆分为两个单独的字段
How to split results from one field into two separate fields in SQL Server 2014
我正在尝试生成一份报告,为我提供每小时、每条车道、每个方向的交通量。
目前我得到的结果:
|TransDate |Hour |Lane| Direction| Count|
---------------------------------------------
|2017-09-05 |1:00 | 1 | NB | 18 |
---------------------------------------------
|2017-09-05 |1:00 | 1 | SB | 12 |
---------------------------------------------
|2017-09-05 |1:00 | 2 | NB | 42 |
---------------------------------------------
|2017-09-05 |1:00 | 2 | SB | 31 |
---------------------------------------------
|2017-09-05 |1:00 | 3 | NB | 7 |
---------------------------------------------
|2017-09-05 |1:00 | 3 | SB | 8 |
---------------------------------------------
我希望得到的结果:
|TransDate |Hour |Lane| NB | SB |
----------------------------------------
|2017-09-05 |1:00 | 1 | 18| 12 |
----------------------------------------
|2017-09-05 |1:00 | 2 | 42| 31 |
----------------------------------------
|2017-09-05 |1:00 | 3 | 7| 8 |
----------------------------------------
到目前为止,我的方法是联合两组代码。一组用于 NB 方向,另一组用于 SB 方向。
到目前为止我的代码:
SELECT CAST(TransDT as DATE) as 'TransDate'
,CAST(DATEPART(Hour, TransDT) as varchar) + ':00' as 'Hour'
,LaneID
,Direction
,COUNT(*) as 'NB_Count'
FROM Traffic_analysis
WHERE cast(TransDT as date) = DATEADD (DAY, -1 , cast(SYSDATETIME() as date))
AND Direction = 'NB'
GROUP BY CAST(TransDT as DATE), DATEPART(Hour, TransDT), LaneID, Direction
ORDER BY CAST(TransDT as DATE), DATEPART(Hour, TransDT), LaneID, Direction
这显然不是正确的方法,因为它不起作用。
我的方法应该是什么才能像第二个那样得到每个方向的计数 table?
您可以如下使用数据透视表:
Select * from
( Select transdate, [hour], lane, direction, [count] from #transdata ) a
pivot (max([count]) for Direction in ([NB],[SB])) p
输出如下:
+------------+------------------+------+----+----+
| transdate | hour | lane | NB | SB |
+------------+------------------+------+----+----+
| 2017-09-05 | 01:00:00.0000000 | 1 | 18 | 12 |
| 2017-09-05 | 01:00:00.0000000 | 2 | 42 | 31 |
| 2017-09-05 | 01:00:00.0000000 | 3 | 7 | 8 |
+------------+------------------+------+----+----+
如果您有方向的动态列表,您可以使用如下动态查询:
Declare @cols1 varchar(max)
Declare @query nvarchar(max)
Select @cols1 = stuff((select Distinct ','+QuoteName(Direction) from #transdata for xml path('')),1,1,'')
Select @query = ' Select * from
( Select transdate, [hour], lane, direction, [count] from #transdata ) a
pivot (max([count]) for Direction in (' + @cols1 + ')) p '
Exec sp_executeSql @query
我正在尝试生成一份报告,为我提供每小时、每条车道、每个方向的交通量。
目前我得到的结果:
|TransDate |Hour |Lane| Direction| Count|
---------------------------------------------
|2017-09-05 |1:00 | 1 | NB | 18 |
---------------------------------------------
|2017-09-05 |1:00 | 1 | SB | 12 |
---------------------------------------------
|2017-09-05 |1:00 | 2 | NB | 42 |
---------------------------------------------
|2017-09-05 |1:00 | 2 | SB | 31 |
---------------------------------------------
|2017-09-05 |1:00 | 3 | NB | 7 |
---------------------------------------------
|2017-09-05 |1:00 | 3 | SB | 8 |
---------------------------------------------
我希望得到的结果:
|TransDate |Hour |Lane| NB | SB |
----------------------------------------
|2017-09-05 |1:00 | 1 | 18| 12 |
----------------------------------------
|2017-09-05 |1:00 | 2 | 42| 31 |
----------------------------------------
|2017-09-05 |1:00 | 3 | 7| 8 |
----------------------------------------
到目前为止,我的方法是联合两组代码。一组用于 NB 方向,另一组用于 SB 方向。
到目前为止我的代码:
SELECT CAST(TransDT as DATE) as 'TransDate'
,CAST(DATEPART(Hour, TransDT) as varchar) + ':00' as 'Hour'
,LaneID
,Direction
,COUNT(*) as 'NB_Count'
FROM Traffic_analysis
WHERE cast(TransDT as date) = DATEADD (DAY, -1 , cast(SYSDATETIME() as date))
AND Direction = 'NB'
GROUP BY CAST(TransDT as DATE), DATEPART(Hour, TransDT), LaneID, Direction
ORDER BY CAST(TransDT as DATE), DATEPART(Hour, TransDT), LaneID, Direction
这显然不是正确的方法,因为它不起作用。
我的方法应该是什么才能像第二个那样得到每个方向的计数 table?
您可以如下使用数据透视表:
Select * from
( Select transdate, [hour], lane, direction, [count] from #transdata ) a
pivot (max([count]) for Direction in ([NB],[SB])) p
输出如下:
+------------+------------------+------+----+----+
| transdate | hour | lane | NB | SB |
+------------+------------------+------+----+----+
| 2017-09-05 | 01:00:00.0000000 | 1 | 18 | 12 |
| 2017-09-05 | 01:00:00.0000000 | 2 | 42 | 31 |
| 2017-09-05 | 01:00:00.0000000 | 3 | 7 | 8 |
+------------+------------------+------+----+----+
如果您有方向的动态列表,您可以使用如下动态查询:
Declare @cols1 varchar(max)
Declare @query nvarchar(max)
Select @cols1 = stuff((select Distinct ','+QuoteName(Direction) from #transdata for xml path('')),1,1,'')
Select @query = ' Select * from
( Select transdate, [hour], lane, direction, [count] from #transdata ) a
pivot (max([count]) for Direction in (' + @cols1 + ')) p '
Exec sp_executeSql @query