0

如何将这三个查询合并为一个,以便可以从此查询创建单个视图?

CREATE VIEW v1 AS
SELECT rp.ProjectName, rp.TaxRegime, rp.CashflowReportProfilePrimaryKey_ReportPrimaryKey FROM ReportProfiles rp
WHERE rp.CashflowReportProfilePrimaryKey_ReportPrimaryKey IS NOT NULL
GROUP BY rp.ProjectName, rp.TaxRegime, rp.CashflowReportProfilePrimaryKey_ReportPrimaryKey

CREATE VIEW v2 AS
SELECT vd.Year, vd.Data, r.ReportPrimaryKey FROM VectorData vd, Reports r
WHERE vd.FK_Vector = r.Pretax_VectorID AND vd.Data != 0 AND LEN(r.Name) < 1

SELECT v1.ProjectName, v1.TaxRegime, v2.Year, FORMAT(v2.Data, 'N', 'nb-NO') FROM v1, v2
WHERE v1.CashflowReportProfilePrimaryKey_ReportPrimaryKey = v2.ReportPrimaryKey
ORDER BY v1.ProjectName, v2.Year
4

1 回答 1

1

您可以像这样使用 CTE 并将其用作新视图的主体:

Create View View1 as 
with  v1 AS
    (
    SELECT 
        rp.ProjectName, 
        rp.TaxRegime, 
        rp.CashflowReportProfilePrimaryKey_ReportPrimaryKey 
    FROM ReportProfiles rp
    WHERE rp.CashflowReportProfilePrimaryKey_ReportPrimaryKey IS NOT NULL
    GROUP BY rp.ProjectName, rp.TaxRegime, rp.CashflowReportProfilePrimaryKey_ReportPrimaryKey
    )
    , v2 AS
    (
    SELECT 
        vd.Year, 
        vd.Data, 
        r.ReportPrimaryKey 
    FROM VectorData vd, Reports r
    WHERE vd.FK_Vector = r.Pretax_VectorID AND vd.Data != 0 AND LEN(r.Name) < 1
    )
    SELECT 
        v1.ProjectName,
        v1.TaxRegime, 
        v2.Year, 
        FORMAT(v2.Data, 'N', 'nb-NO') 
    FROM v1, v2
    WHERE v1.CashflowReportProfilePrimaryKey_ReportPrimaryKey = v2.ReportPrimaryKey
    ORDER BY v1.ProjectName, v2.Year
于 2013-04-16T09:55:32.407 回答