I am having trouble with indexed views, through reading and trial and error I have found several queries are not valid for indexed views or for views referenced by an indexed viewItemsTblID Description1 Item12 Item2InvoicesTblID ItemID Value1 1 10PaymentsTblID ItemID Value1 2 10
Here are two examples of queries to get the data I need but that won't work for an indexed viewSELECT ItemsTbl.ID, SUM(Invoices.Value) AS Invoices, SUM(Payments.Value) AS PaymentsFROM ItemsTbl LEFT OUTER JOIN Payments ON ItemsTbl.ID = Payments.ItemID LEFT OUTER JOIN Invoices ON ItemsTbl.ID = Invoices.ItemIDGROUP BY ItemsTbl.ID
orSELECT dtbl.ItemID, SUM(dtbl.Invoices) AS Invoices, SUM(dtbl.Payments) AS PaymentsFROM (SELECT ItemsTbl.ID as ItemID, Invoices.Value AS Invoices, NULL AS Payments FROM ItemsTbl INNER JOIN Invoices ON ItemsTbl.ID = Invoices.ItemID UNION ALL SELECT ItemsTbl_1.ID as ItemID, NULL AS Invoices, Payments.Value AS Payments FROM ItemsTbl AS ItemsTbl_1 INNER JOIN Payments ON ItemsTbl_1.ID = Payments.ItemID) AS dtblGROUP BY dtbl.ItemID
both will give me the results1 10 NULL2 NULL 10
but neither is valid for an indexed view, the first one because it uses an outer join and the second because it uses a unionDoes anyone know of a way to write a query that will get these results and will work with indexed views?ThanksTravis