当前,我从#temp中获取数据,并从其他表中提取数据。 #temp表格式如下
WorkerID MainDoc SubDoc Value TimeStamp
-------- -------- --------- ----- ---------
1224 Doc1 A1 abc 11:40
1234 Doc1 A2 def 11:40
1224 Doc1 B1 30 11.40
1234 Doc1 B2 40 11:40
1224 Doc1 C1 50 11:40
1234 Doc1 C1 51 11:50
1224 Doc1 C2 60 11:40
1234 Doc1 C2 61 11:50
1235 Doc1 A1 fgf 11:55
1235 Doc1 A2 vbv 11:55
当WorkerID
相同且格式如下时,我需要将此行换成具有最新时间戳值的一行。
WorkerID MainDoc Value(1) Value(2) Value(3) Value(4) Value(5) Value(6)
-------- ------- -------- -------- -------- -------- -------- --------
1234 Doc1 abc def 30 40 51 61
1235 Doc1 fgf vbv NULL NULL NULL NULL
因此,我该怎么做!谢谢!
答案 0 :(得分:-1)
我很抱歉,因为原始答案存在一些逻辑错误,最后我有时间测试一下
CREATE TABLE #MyTable (workerid int, maindoc varchar(5), subdoc varchar(2), value varchar(3), timestamp varchar(5))
insert #MyTable values
(1234, 'Doc1', 'A1', 'abc', '11:40'),
(1234, 'Doc1', 'A2', 'def', '11:40'),
(1234, 'Doc1', 'B1', '30' , '11.40'),
(1234, 'Doc1', 'B2', '40' , '11:40'),
(1234, 'Doc1', 'C1', '50' , '11:40'),
(1234, 'Doc1', 'C1', '51' , '11:50'),
(1234, 'Doc1', 'C2', '60' , '11:40'),
(1234, 'Doc1', 'C2', '61' , '11:50'),
(1235, 'Doc1', 'A1', 'fgf', '11:55'),
(1235, 'Doc1', 'A2', 'vbv', '11:55');
在这种方法中,我的第一步是使用CTE获取ValueNumber并获取每个SubDoc的最后一个值,然后我将其透视。...
WITH subDocs AS (
SELECT DISTINCT ROW_NUMBER() OVER (ORDER BY SubDoc) vn, SubDoc FROM (SELECT DISTINCT SubDoc FROM #MyTable) A
), cte AS (
SELECT
WorkerID,
MainDoc,
T.SubDoc,
Value,
ROW_NUMBER() OVER (PARTITION BY WorkerID, MainDoc, T.SubDoc ORDER BY TimeStamp DESC) AS rn,
vn
FROM #MyTable T
JOIN subDocs S ON T.subdoc = S.subdoc
)
SELECT WorkerID, MainDoc, [1] as [Value(1)], [2] as [Value(2)], [3] as [Value(3)], [4] as [Value(4)], [5] as [Value(5)], [6] as [Value(6)]
FROM (
SELECT WorkerID, Maindoc, vn, Value FROM cte WHERE rn = 1
) Source
PIVOT (
MAX(Value) FOR vn IN ([1],[2],[3],[4],[5],[6])
) PVT
但是很可能您想要的列数不是固定的,您需要使用动态SQL来获取它
DECLARE @SQL NVARCHAR(MAX);
DECLARE @SubDocCount INT
DECLARE @ColumnNames NVARCHAR(MAX)
DECLARE @PivotValues NVARCHAR(MAX)
SELECT TOP 1 @SubDocCount = COUNT(DISTINCT Subdoc) FROM #MyTable
;WITH cols AS (
SELECT 1 as num
UNION ALL
SELECT num + 1
FROM cols WHERE num < @SubDocCount
)
SELECT
@ColumnNames = STUFF(
(SELECT ',' + QUOTENAME(CAST(num AS VARCHAR)) + ' AS ' + QUOTENAME('Value' + QUOTENAME(CAST(num AS VARCHAR),'('))
FROM cols
FOR XML PATH (''))
, 1, 1, ''),
@PivotValues = STUFF(
(SELECT ',' + QUOTENAME(CAST(num AS VARCHAR))
FROM cols
FOR XML PATH (''))
, 1, 1, '')
SET @SQL = N'
WITH subDocs AS (
SELECT DISTINCT ROW_NUMBER() OVER (ORDER BY SubDoc) vn, SubDoc FROM (SELECT DISTINCT SubDoc FROM #MyTable) A
), cte AS (
SELECT
WorkerID,
MainDoc,
T.SubDoc,
Value,
ROW_NUMBER() OVER (PARTITION BY WorkerID, MainDoc, T.SubDoc ORDER BY TimeStamp DESC) AS rn,
vn
FROM #MyTable T
JOIN subDocs S ON T.subdoc = S.subdoc
)
SELECT WorkerID, MainDoc, ' + @ColumnNames + '
FROM (
SELECT WorkerID, Maindoc, vn, Value FROM cte WHERE rn = 1
) Source
PIVOT (
MAX(Value) FOR vn IN (' + @PivotValues + ')
) PVT'
EXEC (@SQL)
注意,在给定的预期结果中,我假设1224和1234为错字,示例输入中有3个不同的WorkerID