我正在尝试将行转换为SQL Server 2000中的列。虽然所有行都转换为必需的列但我无法将值转换为相应的列。
我的代码是:
DROP TABLE prePivot1
DROP TABLE results1
CREATE TABLE prePivot1 (
[StudentId] uniqueidentifier
, [Sub_Abbr] Varchar(100)
, [Total_marks] MONEY
)
CREATE TABLE results1 (
[StudentId] uniqueidentifier
)
INSERT INTO prePivot1
SELECT '{4FD7CBBA-1621-4102-B4A3-000BF92E2F6E}', 'ENG', 55
UNION SELECT '{4FD7CBBA-1621-4102-B4A3-000BF92E2F6E}', 'MBEN', 90
UNION SELECT '{4FD7CBBA-1621-4102-B4A3-000BF92E2F6E}', 'ECO', 80
UNION SELECT '{4FD7CBBA-1621-4102-B4A3-000BF92E2F6E}', 'PSc', 45
UNION SELECT '{4FD7CBBA-1621-4102-B4A3-000BF92E2F6E}', 'PSY', 23
UNION SELECT '{AD288712-5C97-446B-8AFE-003FC845B56B}', 'ENG', 90
UNION SELECT '{AD288712-5C97-446B-8AFE-003FC845B56B}', 'ECO', 44
UNION SELECT '{AD288712-5C97-446B-8AFE-003FC845B56B}', 'PSc', 45
UNION SELECT '{AD288712-5C97-446B-8AFE-003FC845B56B}', 'BST', 23
UNION SELECT '{AD288712-5C97-446B-8AFE-003FC845B56B}', 'ASS', 80
UNION SELECT '{AD288712-5C97-446B-8AFE-003FC845B56B}', 'PSY', 93
DECLARE @sql VARCHAR(8000)
DECLARE @pivot varchar(10)
DECLARE pivotCursor CURSOR LOCAL READ_ONLY FOR
SELECT DISTINCT
[Sub_abbr]
FROM
prePivot1
OPEN pivotCursor
FETCH NEXT FROM pivotCursor INTO @pivot
WHILE (@@FETCH_STATUS = 0) BEGIN
SET @sql = '
ALTER TABLE results1 ADD [' + CAST(@pivot AS VARCHAR(10)) + '] INT'
EXEC (@sql)
FETCH NEXT FROM pivotCursor INTO @pivot
END
CLOSE pivotCursor
INSERT INTO results1 ([studentId])
SELECT DISTINCT [StudentId] FROM prePivot1
OPEN pivotCursor
FETCH NEXT FROM pivotCursor INTO @pivot
WHILE (@@FETCH_STATUS = 0) BEGIN
SET @sql = '
UPDATE results1
SET
[' + CAST(@pivot AS VARCHAR(10)) + '] = pp.[total_marks]
FROM
prePivot1 pp
WHERE
pp.[total_marks] = ' + CAST(@pivot AS VARCHAR(10)) + '
AND pp.[studentId] = results1.[studentId]'
EXEC (@sql)
FETCH NEXT FROM pivotCursor INTO @pivot
END
CLOSE pivotCursor
DEALLOCATE pivotCursor
SELECT * FROM results1
请帮助我如何获得预期的结果。 提前感谢你。
答案 0 :(得分:0)
这里的谓词存在一些问题:
pp.[total_marks] = ' + CAST(@pivot AS VARCHAR(10)) + '
这是打印出动态sql字符串时的样子。请注意,它会导致prePivot1.total_marks列上的连接到results1.ECO列,这不是您想要的。
UPDATE
results1
SET
[ECO] = pp.[total_marks]
FROM
prePivot1 AS pp
WHERE
pp.[total_marks] = ECO
AND pp.[studentId] = results1.[studentId]
更正版本(您也不需要转换变量):
pp.[Sub_Abbr] = ''' + @pivot + '''
一个稍微不那么容易出错的解决方案就是参数化你的动态sql:
-- Change @sql --> nvarchar(4000)
SET @sql = N'
UPDATE
results1
SET
' + QUOTENAME(@pivot) + N' = pp.[total_marks]
FROM
prePivot1 AS pp
WHERE
pp.[Sub_Abbr] = @Sub_Abbr
AND pp.[studentId] = results1.[studentId];';
EXEC sp_executesql
@stmt = @sql,
@params = N'@Sub_Abbr varchar(10)',
@Sub_Abbr = @pivot;
这是我在sql2k中用于仅供参考的一般模式: (我在这里假设StudentId,Sub_Abbr是唯一的,虽然架构没有明确证实这一点)
SELECT
[StudentId],
MAX(CASE WHEN [Sub_Abbr] = 'ASS' THEN [Total_marks] ELSE NULL END) AS [ASS],
MAX(CASE WHEN [Sub_Abbr] = 'BST' THEN [Total_marks] ELSE NULL END) AS [BST],
MAX(CASE WHEN [Sub_Abbr] = 'ECO' THEN [Total_marks] ELSE NULL END) AS [ECO],
MAX(CASE WHEN [Sub_Abbr] = 'ENG' THEN [Total_marks] ELSE NULL END) AS [ENG],
MAX(CASE WHEN [Sub_Abbr] = 'MBEN' THEN [Total_marks] ELSE NULL END) AS [MBEN],
MAX(CASE WHEN [Sub_Abbr] = 'PSc' THEN [Total_marks] ELSE NULL END) AS [PSc],
MAX(CASE WHEN [Sub_Abbr] = 'PSY' THEN [Total_marks] ELSE NULL END) AS [PSY]
FROM
[dbo].[prePivot1]
GROUP BY
[StudentId];