0
A
回答
0
您可以使用動態SQL查詢。
查詢
declare @sql as varchar(max);
select @sql = 'select ' + stuff((
select ', max(case StudentID when '
+ cast(t.StudentID as varchar(10))
+ ' then StudentKey end) as StudentID'
+ cast(t.StudentID as varchar(10))
+', max(case StudentID when ' + cast(t.StudentID as varchar(10))
+ ' then StudentName end) as StudentName'
+ cast(t.StudentID as varchar(10))
from (select distinct top 3 * from studentTable order by StudentID)t
for xml path('')
), 1, 2, '') + ' from studentTable;';
exec(@sql);
而且這會給結果1個StudentId然後StudentName等的coulmn順序。有些事情如下。
結果
+------------+--------------+------------+--------------+------------+--------------+
| StudentID1 | StudentName1 | StudentID2 | StudentName2 | StudentID3 | StudentName3 |
+------------+--------------+------------+--------------+------------+--------------+
| 125 | A | 225 | B | 325 | C |
+------------+--------------+------------+--------------+------------+--------------+
如果你想要的結果像所有的studentId列第一則studentName列。然後
查詢
declare @sql as varchar(max);
select @sql = 'select ' + stuff((
select ', max(case StudentID when '
+ cast(t.StudentID as varchar(10))
+ ' then StudentKey end) as StudentID'
+ cast(t.StudentID as varchar(10))
from (select distinct top 3 * from studentTable order by StudentID)t
for xml path('')
), 1, 2, '')
+ ','
+ stuff((
select ', max(case StudentID when '
+ cast(t.StudentID as varchar(10))
+ ' then StudentName end) as StudentName'
+ cast(t.StudentID as varchar(10))
from (select distinct top 3 * from studentTable order by StudentID)t
for xml path('')
), 1, 2, '')
+ ' from studentTable;';
exec(@sql);
結果
+------------+------------+------------+--------------+--------------+--------------+
| StudentID1 | StudentID2 | StudentID3 | StudentName1 | StudentName2 | StudentName3 |
+------------+------------+------------+--------------+--------------+--------------+
| 125 | 225 | 325 | A | B | C |
+------------+------------+------------+--------------+--------------+--------------+
0
您需要使用PIVOT。它應該像下面這樣。如果你不知道PIVOT是如何工作的,可以嘗試在線學習Excel Pivot,讓你先熟悉它的邏輯。
WITH PivotData AS
(
SELECT
AssignmentName,
StudentName,
Grade
FROM TableName
)
SELECT
StudentName,
Assignment1,
Assignment2,
Assignment3
FROM PivotData
PIVOT
(
SUM(Grade)
FOR AssignmentName
IN (Assignment1, Assignment2, Assignment3)
) AS PivotResult
ORDER BY StudentName
相關問題
- 1. 將多行轉換爲一列SQL Server 2008
- 2. SQL Server將列轉換爲行
- 3. 將行轉換爲列SQL Server,T-SQL
- 4. 轉換行到列在SQL Server 2008
- 5. 轉換行到列SQL Server 2008中
- 6. 如何將行值轉換爲SQL Server 2008中的列標題?
- 7. 將行轉換爲SQL Server 2008中的列
- 8. 使用sql server 2008將行轉換爲列?
- 9. 如何在Sql Server 2008 R2中將列轉換爲行?
- 10. 如何在sql server 2008中將行值轉換爲列?
- 11. 將日期轉換爲列的行 - SQL Server 2008
- 12. 將SQL Server 2008轉換爲2000
- 13. 將varchar轉換爲datetime的SQL Server 2008
- 14. 將Postgis錶轉換爲SQL Server 2008
- 15. SQL Server 2008將varchar轉換爲datetime
- 16. 將SQL Server 2008轉換爲MySQL
- 17. SQL Server將列轉換爲標識列
- 18. SQL Server將行轉換爲列
- 19. 將列轉換爲SQL Server中的行
- 20. 將行轉換爲列SQL Server 2012
- 21. 將行轉換爲列sql server
- 22. SQL Server- PIVOT表。將行轉換爲列
- 23. SQL Query將行轉換爲列 - 重新編寫
- 24. 將SQL列轉換爲行
- 25. SQL Server - 將多個列轉換爲多個行
- 26. 將存儲過程從SQL Server 6.5轉換爲SQL Server 2008
- 27. 如何將SQL Server 2008 R2數據庫轉換爲SQL Server 2012?
- 28. 如何將SQL Server 2008 .mdf文件轉換爲SQL Server 2012?
- 29. 將sql server 2008數據庫轉換爲sql server 2005
- 30. SQL Server 2008 R2 - 如何將多點轉換爲線?
* T-SQL的動態支點*是你需要搜索的內容。 –
請檢查此:http://stackoverflow.com/questions/15745042/efficiently-convert-rows-to-columns-in-sql-server – TheGameiswar
直接在帖子中添加圖像,而不是作爲鏈接 – Dave