0

我在 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 创建的。有解决方法吗?

4

2 回答 2

0

你可以尝试类似的东西

SELECT  VendorId,
        AVG(CASE
            WHEN IncomeDay = 'MON'
                THEN IncomeAmount
            ELSE NULL
        END) [MON],
        AVG(CASE
            WHEN IncomeDay = 'TUE'
                THEN IncomeAmount
            ELSE NULL
        END) [TUE],
        AVG(CASE
            WHEN IncomeDay = 'WED'
                THEN IncomeAmount
            ELSE NULL
        END) [WED],
        AVG(CASE
            WHEN IncomeDay = 'THU'
                THEN IncomeAmount
            ELSE NULL
        END) [THU],
        AVG(CASE
            WHEN IncomeDay = 'FRI'
                THEN IncomeAmount
            ELSE NULL
        END) [FRI],
        AVG(CASE
            WHEN IncomeDay = 'SAT'
                THEN IncomeAmount
            ELSE NULL
        END) [SAT],
        AVG(CASE
            WHEN IncomeDay = 'SUN'
                THEN IncomeAmount
            ELSE NULL
        END) [SUN]
FROM    #DailyIncome
GROUP BY VendorId

请注意,我使用 NULLS 而不是 0 的原因是由于 PIVOT 功能。

使用 PIVOT 和 UNPIVOT

当聚合函数与 PIVOT 一起使用时,计算聚合时不考虑 value 列中是否存在任何空值。

如果您使用 0 而不是 NULL 运行查询,您会发现不同的结果

于 2012-08-16T04:23:28.750 回答
0

枢轴更干净,代码行更少。语法有点不对劲。试试这个作为你的最终语句,它将语句包装在一个 select 子句中:

SELECT * FROM 
(
SELECT *
FROM #DailyIncome
)Data
pivot(avg(IncomeAmount) FOR IncomeDay IN (
        [MON]
    ,[TUE]
    ,[WED]
    ,[THU]
    ,[FRI]
    ,[SAT]
    ,[SUN]
    ))

AS 平均每日收入

于 2012-08-16T17:19:11.973 回答