我在 SQL Server 中找到了此链接数据透视表。一个简单的示例。并将其转换为临时表。但是,我收到一个错误“'(' 附近的语法不正确。”你能帮我解决这个问题吗?下面是代码:
IF OBJECT_ID('tempdb..#DailyIncome') IS NOT NULL
BEGIN
drop table #DailyIncome
END
create table #DailyIncome
(
VendorId nvarchar(10)
, IncomeDay nvarchar(10)
, IncomeAmount int
)
insert into #DailyIncome values ('SPIKE', 'FRI', 100)
insert into #DailyIncome values ('SPIKE', 'MON', 300)
insert into #DailyIncome values ('FREDS', 'SUN', 400)
insert into #DailyIncome values ('SPIKE', 'WED', 500)
insert into #DailyIncome values ('SPIKE', 'TUE', 200)
insert into #DailyIncome values ('JOHNS', 'WED', 900)
insert into #DailyIncome values ('SPIKE', 'FRI', 100)
insert into #DailyIncome values ('JOHNS', 'MON', 300)
insert into #DailyIncome values ('SPIKE', 'SUN', 400)
insert into #DailyIncome values ('JOHNS', 'FRI', 300)
insert into #DailyIncome values ('FREDS', 'TUE', 500)
insert into #DailyIncome values ('FREDS', 'TUE', 200)
insert into #DailyIncome values ('SPIKE', 'MON', 900)
insert into #DailyIncome values ('FREDS', 'FRI', 900)
insert into #DailyIncome values ('FREDS', 'MON', 500)
insert into #DailyIncome values ('JOHNS', 'SUN', 600)
insert into #DailyIncome values ('SPIKE', 'FRI', 300)
insert into #DailyIncome values ('SPIKE', 'WED', 500)
insert into #DailyIncome values ('SPIKE', 'FRI', 300)
insert into #DailyIncome values ('JOHNS', 'THU', 800)
insert into #DailyIncome values ('JOHNS', 'SAT', 800)
insert into #DailyIncome values ('SPIKE', 'TUE', 100)
insert into #DailyIncome values ('SPIKE', 'THU', 300)
insert into #DailyIncome values ('FREDS', 'WED', 500)
insert into #DailyIncome values ('SPIKE', 'SAT', 100)
insert into #DailyIncome values ('FREDS', 'SAT', 500)
insert into #DailyIncome values ('FREDS', 'THU', 800)
insert into #DailyIncome values ('JOHNS', 'TUE', 600)
SELECT * FROM #DailyIncome
SELECT *
FROM #DailyIncome
pivot(avg(IncomeAmount) FOR IncomeDay IN (
[MON]
,[TUE]
,[WED]
,[THU]
,[FRI]
,[SAT]
,[SUN]
)) AS AvgIncomePerDay
多谢你们!
[更新]
根据评论,数据库是使用 SQL Server 2000 创建的。有解决方法吗?