使用"选择语句"而不是"动态列"在PIVOT上

时间:2015-09-23 05:05:24

标签: sql pivot

这是我对pivot

的当前查询
SELECT CORNo, [0001], [0002], [0003]
FROM 
    (
        Select InvoiceNo, CORNo as CORNo, Amount
        from InvoiceItems II 
        left join Invoices I on I.InvoiceID = II.InvoiceID 
        left join CORProjects CPI on CPI.CORProjectID = II.ReferenceID and CPI.ProjectID = I.ProjectID
        LEFT JOIN Projects P ON P.ProjectID = CPI.ProjectID
        where II.ReferenceID = CPI.CORProjectID and I.InvoiceType = 2
        and P.ProjectID = 913 
    ) AS t1 
PIVOT 
(
    MAX(Amount) 
    FOR InvoiceNo IN ( [0001], [0002], [0003]) 
) AS t2

我想做的是不使用动态列 ([0001],[0002],[0003])并在" IN"中使用选择查询子句。

SELECT CORNo, select distinct(InvoiceNo) from Invoices where ProjectID = 913
FROM 
    (
        Select InvoiceNo, CORNo as CORNo, Amount
        from InvoiceItems II 
        left join Invoices I on I.InvoiceID = II.InvoiceID 
        left join CORProjects CPI on CPI.CORProjectID = II.ReferenceID and CPI.ProjectID = I.ProjectID
        LEFT JOIN Projects P ON P.ProjectID = CPI.ProjectID
        where II.ReferenceID = CPI.CORProjectID and I.InvoiceType = 2
        and P.ProjectID = 913 
    ) AS t1 
PIVOT 
(
    MAX(Amount) 
    FOR InvoiceNo IN (select distinct(InvoiceNo) from Invoices where ProjectID = 913) 
) AS t2

当我尝试使用select查询时。我遇到了一个错误。 PIVOT上是否可以选择声明?谢谢。

0 个答案:

没有答案
相关问题