1

我有以下带有派生表的 SQL 查询,因此我无法创建索引视图的 CLUSTERED INDEX。所以我必须将派生表转换为普通连接。

SELECT
......
FROM 
dbo.invoice i

LEFT JOIN 
(
    SELECT SUM(amount) as servicesamount
    ,ise.invoiceuid
    ,ise.episodeuid
    ,ise.memberuid
    ,ev.caseuid

    FROM dbo.invoice_services ise
    JOIN dbo.invoice inv on inv.invoiceuid = ise.invoiceuid
    JOIN dbo.event ev ON ev.eventuid = ise.episodeuid -- JOIN IN ORDER TO BRING EVENT INVOCIES ONLY (DENTAL INVOICES COULD EXIST)
    GROUP BY ise.invoiceuid,ise.episodeuid,ise.memberuid,ev.caseuid

) ise ON ise.invoiceuid = i.invoiceuid

关于如何转换的任何线索

LEFT JOIN 
    (
        SELECT SUM(amount) as servicesamount
        ,ise.invoiceuid
        ,ise.episodeuid
        ,ise.memberuid
        ,ev.caseuid

        FROM dbo.invoice_services ise
        JOIN dbo.invoice inv on inv.invoiceuid = ise.invoiceuid
        JOIN dbo.event ev ON ev.eventuid = ise.episodeuid -- JOIN IN ORDER TO BRING EVENT INVOCIES ONLY (DENTAL INVOICES COULD EXIST)
        GROUP BY ise.invoiceuid,ise.episodeuid,ise.memberuid,ev.caseuid

    ) ise

能正常加入吗?

欣赏它!

4

1 回答 1

0

你可以试试这个

SELECT ...
  ,SUM(amount) as servicesamount
  ,ise.invoiceuid
  ,ise.episodeuid
  ,ise.memberuid
  ,ev.caseuid
  ,...
FROM dbo.invoice inv 
 LEFT JION dbo.invoice_services ise ON inv.invoiceuid = ise.invoiceuid
 LEFT JOIN dbo.event ev ON ev.eventuid = ise.episodeuid
GROUP BY inv..., ise.invoiceuid,ise.episodeuid,ise.memberuid,ev.caseuid
于 2013-02-26T09:03:50.310 回答