SQL Server - 在带有Pivot的VARCHAR列上使用AVG

时间:2015-08-31 19:47:00

标签: sql-server pivot

我尝试在AVG列上使用VARCHAR,但我不断从SQL服务器收到错误:

SELECT
    *
FROM 
    PivotData
PIVOT 
    (AVG(CAST(ClinRefResultValue AS decimal))
     FOR ClinicalRefSensorName IN ([A], [B], [C], [D], [E])
    ) AS PVTTable

我得到的错误是:

  

Msg 102,Level 15,State 1,Line 95
  '('。

附近的语法不正确

我无法弄清楚我做错了什么!

来源数据:

With PivotData AS
(
    select 
        [dbo].[ClinRefFile].[DonorID], 
        [dbo].[ClinRefFile].[Sample], 
        [dbo].[ClinRefSensorMaster].[ClinicalRefSensorName],
        [dbo].[ClinRefResultDetail].ClinRefTime,
        [dbo].[ClinRefResultDetail].ClinRefResultValue
    from 
        [dbo].[ClinRefFile],
        [dbo].[ClinRefFileTypeMaster],
        [dbo].[ClinRefResultDetail],
        [dbo].[ClinRefSensorMaster]
    where 
        [dbo].[ClinRefFile].[ClinRefFileID] = [dbo].[ClinRefResultDetail].[ClinRefFileID] 
        and [dbo].[ClinRefFile].[ClinRefTypeID] = [dbo].[ClinRefFileTypeMaster].[ClinRefTypeID] 
        and [dbo].[ClinRefResultDetail].[ClinRefSensorID] = [dbo].[ClinRefSensorMaster].[ClinicalRefSensorID] 
        and [dbo].[ClinRefResultDetail].ClinRefTime = 'Average'
)

0 个答案:

没有答案