SSMS查询 - 关键字' FOR'附近的语法不正确

时间:2017-11-20 18:08:20

标签: sql sql-server pivot ssms

我在下面的查询中收到错误。子查询可以正常工作; 3列,ID,类型,金额。根据SSMS"关于关键字' FOR'"

,我在使用Pivot语法时遇到问题。

查询在SO中反映了一个问题,我看到了很多其他类似的问题,但我似乎无法找到问题。我尝试过使用括号,在子查询中删除联接并在数据轮转后加入但到目前为止没有任何工作。

SELECT
dboBillingAdjustments.CMSId
,DataPivot.Certification
,[DataPivot.Contractor Supplement]

FROM
    (
        SELECT
            Businesses.CMSId
            ,BillingAdjustments.Type
            ,BillingAdjustments.ContractorAmount

FROM BillingAdjustments
            JOIN BillingAdjustmentTypes on BillingAdjustmentTypes.Id = BillingAdjustments.TypeId
        JOIN Businesses on Businesses.UId = BillingAdjustments.BusinessId

    WHERE 
        DateKey = 20171104 AND
        IsSettled = 1

) as dboBillingAdjustments

PIVOT
(
    BillingAdjustments.ContractorAmount FOR Type in ([Certification], [Contractor Supplement])
) as DataPivot

1 个答案:

答案 0 :(得分:0)

使用PIVOT,您需要一个聚合函数MAX(),MIN(),AVG()... 例如:

PIVOT
(
    MAX(BillingAdjustments.ContractorAmount) 
    FOR Type in ([Certification], [Contractor Supplement])
) as DataPivot