我已经非常努力地了解如何在SQL中创建数据透视表,但我无法管理它!
我有以下专栏:
link_id route_section date_1 StartHour AvJT data_source
....... ............. ....... ........... ...... ............
拥有600,000行数据。
我需要在以下数据透视表中使用它们;
date_1
StartHour作为列标题link_id
作为行标题AvJT
作为数据data_source
=' 1'作为过滤器。PIVOT TABLE
Link_ID
date_1 StartHour 00001a 000002a 000003a 000004a
20/01/2014 8 456 4657 556 46576
21/01/2014 8 511 4725 601 52154
22/01/2014 8 468 4587 458 47585
23/01/2014 8 456 4657 556 46576
24/01/2014 8 456 4657 556 46576
25/01/2014 8 456 4657 556 46576
26/01/2014 8 456 4657 556 46576
我设法获得了以下代码,但这只是我的日期_1作为列标题而不是StartHour,或者过滤器为date_source =' 1'。
Use [C1_20132014]
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
--Get distinct values of the PIVOT Column
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME(Link_ID)
FROM (SELECT DISTINCT Link_ID FROM C1_May_Routes) AS Link_ID
--Prepare the PIVOT query using the dynamic
SET @DynamicPivotQuery =
N'SELECT Date_1, ' + @ColumnName + '
FROM C1_May_Routes
PIVOT(SUM(AvJT)
FOR Link_ID IN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery
感谢您的帮助,
亨利
答案 0 :(得分:1)
在这里,您将选择列中的值以显示为pivot
中的列DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + AvJT + ']', '[' + AvJT + ']')
FROM (SELECT DISTINCT AvJT FROM YourTable) PV
ORDER BY AvJT
现在转动查询
DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT * FROM
(
SELECT date_1, StartHour,AvJT, data_source
FROM YourTable
) x
PIVOT
(
-- Values in each dynamic column
SUM(data_source)
FOR AvJT IN (' + @cols + ')
) p;'
EXEC SP_EXECUTESQL @query
如果要将列名称设置为动态,可以执行以下查询
SELECT DATE_1,STARTHOUR,
MIN(CASE WHEN AvJT='00001a' THEN data_source END) [00001a],
MIN(CASE WHEN AvJT='00002a' THEN data_source END) [00002a],
MIN(CASE WHEN AvJT='00003a' THEN data_source END) [00003a],
MIN(CASE WHEN AvJT='00004a' THEN data_source END) [00004a]
FROM YOURTABLE
GROUP BY DATE_1,STARTHOUR
编辑:
我正在更新您的更新问题。
声明用于过滤data_source
DECLARE @DATASOURCE VARCHAR(20) = '1'
您可以使用其他格式来获取数据透视列
,而不是QUOTENAME
DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + Link_ID + ']', '[' + Link_ID + ']')
FROM (SELECT DISTINCT Link_ID FROM C1_May_Routes WHERE data_source=@DATASOURCE) PV
ORDER BY Link_ID
现在转动
DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT * FROM
(
-- We will select the data that has to be shown for pivoting
-- with filtered data_source
SELECT date_1, StartHour,AvJT, Link_ID
FROM C1_May_Routes
WHERE data_source = '+@DATASOURCE+'
) x
PIVOT
(
-- Values in each dynamic column
SUM(AvJT)
-- Select columns from @cols
FOR Link_ID IN (' + @cols + ')
) p;'
EXEC SP_EXECUTESQL @query
答案 1 :(得分:0)
交叉表就是这样的。 FWIW,我建议使用比00001a更好的列名。为列名提供一些含义,以便更容易使用。
with SortedData as
(
SELECT date_1
, StartHour
, AvJT
, data_source
, ROW_NUMBER() over (partition by date_1 order by AvJT) AS RowNum
FROM YourTable
)
select date_1
, StartHour
, MAX(case when RowNum = 1 then AvJT end) as [00001a]
, MAX(case when RowNum = 2 then AvJT end) as [00002a]
, MAX(case when RowNum = 3 then AvJT end) as [00003a]
, MAX(case when RowNum = 4 then AvJT end) as [00004a]
from SortedData