我是SQL的新手,可以使用一些帮助。我有一张时间表数据表,每行有一个单独的时间表。每个时间表都有一个包含jobcode1到jobcode16的列,它存储一个表示作业代码的字符串。每个都有相应的TotalJob1到TotalJob16。
我已经设法在JobCode1上创建了一个数据透视表,每个作业的列和TotalJob1的总数都没有问题。我使用this来构建它。
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName = ISNULL(@ColumnName + ',','') + QUOTENAME(TS_Job1Code)
FROM (SELECT DISTINCT TS_Job1Code FROM
dbo.timesheetData) as timesheetdata
SET @DynamicPivotQuery =
N'SELECT VolumeID, ' + @ColumnName + '
FROM dbo.timesheetData
PIVOT(SUM(TS_TotalJob1)
FOR TS_Job1Code IN (' + @ColumnName + ')) AS PVTTable'
EXEC sp_executesql @DynamicPivotQuery
我正在努力迭代其他Job列并将它们合并到一个大的数据透视表中,并希望有人能够给我一个指针?
我的想法是尝试重复这一步16次,但我认为这甚至不是正确的方式。
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
DECLARE @N AS INT
DECLARE @NCHAR AS NVARCHAR(MAX)
SET @N = 1
WHILE @N < 17
BEGIN
SET @NCHAR = CAST(@N as VARCHAR)
SELECT @ColumnName = ISNULL(@ColumnName + ',','') + QUOTENAME(('TS_Job' +
@NCHAR + 'Code'))
FROM (SELECT DISTINCT ('TS_Job' + @NCHAR + 'Code') FROM
dbo.timesheetData) as timesheetdata
SET @DynamicPivotQuery =
N'SELECT ' + @ColumnName + '
FROM dbo.timesheetData
PIVOT(SUM(TS_TotalJob' + @NCHAR + ')
FOR TS_Job' + @NCHAR + 'Code IN (' + @ColumnName + ')) AS PVTTable'
EXEC sp_executesql @DynamicPivotQuery
SET @N = @N + 1
END
EXEC sp_executesql @SQL
原始
+-------------+----------+----------+----------+-----------+-----------+-----------+
| TimesheetID | JobCode1 | JobCode2 | JobCode3 | TotalJob1 | TotalJob2 | TotalJob3 |
+-------------+----------+----------+----------+-----------+-----------+-----------+
| 1 | J1 | J3 | | 10 | 9 | |
+-------------+----------+----------+----------+-----------+-----------+-----------+
| 2 | J2 | J1 | J3 | 5 | 5 | 5 |
+-------------+----------+----------+----------+-----------+-----------+-----------+
| 3 | J2 | | | 6 | 3 | 1 |
+-------------+----------+----------+----------+-----------+-----------+-----------+
我想要实现的目标
+-------------+----+----+----+----+----+
| TimesheetID | J1 | J2 | J3 | J4 | J6 |
+-------------+----+----+----+----+----+
| 1 | 10 | | 9 | | |
+-------------+----+----+----+----+----+
| 2 | 5 | 5 | 5 | | |
+-------------+----+----+----+----+----+
| 3 | | 6 | | 3 | 1 |
+-------------+----+----+----+----+----+
答案 0 :(得分:0)
它会变得非常复杂,但你可以做的一件事就是取消你的数据,看起来像这样:
TimesheetId JobCode JobTotal
1 J1 10
1 J3 9
2 J1 5
....
然后PIVOT派生表来获得你想要的结果。
答案 1 :(得分:0)
与提到的标签一样,您可以先将数据取消隐藏,然后再将其旋转。
您可以使用CROSS APPLY和VALUES将表格展开到临时表中。
SELECT ca.*
INTO #temp
FROM timesheet
CROSS APPLY (VALUES
(TimesheetID, JobCode1, TotalJob1),
(TimesheetID, JobCode2, TotalJob2),
(TimesheetID, JobCode3, TotalJob3)
) ca(TimesheetID, JobCode, TotalJob)
这给你一个像
这样的表格TimesheetID JobCode TotalJob
----------- ------- -----------
1 J1 10
1 J3 9
1 NULL NULL
2 J2 5
2 J1 5
2 J3 5
3 J2 6
3 NULL 3
3 NULL 1
不确定您的实际数据中是否有空作业代码..但您可以使用上面的Where ca.JobCode IS NOT NULL
消除它们
然后从临时表
创建动态列字符串DECLARE @JobCodes nvarchar(MAX)
SELECT @JobCodes = COALESCE(@JobCodes + ',','') + QUOTENAME(JobCode)
FROM #temp
GROUP BY JobCode
ORDER BY JobCode
然后构建动态数据透视表。而不是创建全局临时表,只需在数据透视查询之前使用相同的查询。
DECLARE @Sql nvarchar(max)
SET @Sql = N'
SELECT TimeSheetID,' + @JobCodes
+ 'FROM (
SELECT ca.*
FROM timesheet
CROSS APPLY (VALUES
(TimesheetID, JobCode1, TotalJob1),
(TimesheetID, JobCode2, TotalJob2),
(TimesheetID, JobCode3, TotalJob3)
) ca(TimesheetID, JobCode, TotalJob)
) t
PIVOT (
SUM(TotalJob)
FOR JobCode IN (' + @JobCodes + ')
) p'
EXEC sp_executesql @Sql
答案 2 :(得分:-1)
这里有许多动态枢轴问题。这是一个很好的例子:SQL Server dynamic PIVOT query?