We have an invoice, a invoice detail and a order table and the tables are linked by the invoice detail rows because the invoice details are grouped by delivery date so a invoice often covers multiple order numbers.
Now I would like to build a view that would display the order number if there is only one order involved in the invoice by using a subselect of some kind.
I came up with this one but it still generates an error reporting that the subquery return more than one result
SELECT Invoice.Id, Invoice.TotalAmount,
(SELECT DISTINCT OrderId FROM InvoiceDetail
WHERE InvoiceDetail.InvoiceId = Invoice.Id
GROUP BY OrderId HAVING COUNT(DISTICT OrderId) = 1) AS OrderId
FROM Invoice
Any ideas to get this to work?
How about: