r/SQLOptimization • • Feb 28 '23

how do I ingrate this CTE?

with highque as(

	select max(ExtendedPrice) highest

	from Sales.InvoiceLines il

	join Sales.Invoices i on il.InvoiceID = i.InvoiceID

	where (InvoiceDate between '1/1/2013' and '12/31/2013')

	group by i.CustomerID

	)

select InvoiceDate, CustomerName

from Sales.Invoices i

join Sales.Customers c on c.CustomerID = i.CustomerID

where (InvoiceDate between '1/1/2013' and '12/31/2013')

order by CustomerName

the CTE finds the largest invoice 2013, the query after finds the customer name and date of invoice, how do I connect the largest invoice to the customer and the date they invoiced?

4 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/planetmatt Mar 01 '23

You've posted your desired output. It would help to see the table structure to be honest.

So to be clear, you want to see the largest invoice value for each company on each day?