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