无法透视表

时间:2012-08-16 04:04:50

标签: sql tsql

我找到了此链接Pivot tables in SQL Server. A simple sample.并将其转换为临时表。但是,我收到一个错误“'附近的语法不正确'('。”你可以帮我吗?下面是代码:

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

谢谢你们!

[UPDATE]

根据评论,数据库是使用SQL Server 2000创建的。是否有解决方法?

2 个答案:

答案 0 :(得分: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功能。

来自Using PIVOT and UNPIVOT

  

当聚合函数与PIVOT一起使用时,任何null都存在   计算a时,不考虑值列中的值   聚合

如果要使用0而不是NULL运行查询

,将会发现不同的结果

答案 1 :(得分:0)

枢轴更干净,代码行更少。语法只是有点偏。尝试使用此语句作为最终语句,它将语句包装在select子句中:

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

AS AvgIncomePerDay