复制代码 代码如下:
SELECT * FROM
(SELECT UID,Name, Score,ScoreName FROM Score,[User] WHERE Score.UID=[User].ID) AS SourceTable
PIVOT(AVG(Score)FOR ScoreName IN ([英语], [数学])) AS a
复制代码 代码如下:
DECLARE @s NVARCHAR(4000)
SELECT @s = ISNULL(@s + ',', '') + QUOTENAME(ScoreName)
FROM (select distinct ScoreName from Score) as A ---列名不要重复
Declare @sql NVARCHAR(4000)
SET @sql='
select r.* from
(select UID,Name,ScoreName,Score from Score,[User] where Score.UID=[User].ID) as t
pivot
(
max(t.Score)
for t.ScoreName in ('+@s+')
) as r'
EXEC( @sql)
复制代码 代码如下:
select
row_number() OVER(ORDER BY [User].ID) as 编号,
UID as 用户编号,
Name as 姓名,
max(case ScoreName when '英语' then Score else 0 end) 英语,
max(case ScoreName when '数学' then Score else 0 end) 数学
from Score,[User] WHERE Score.UID=[User].ID
group by UID,[User].ID,Name
新闻热点
疑难解答