Skip to content
Advertisement

SQL selecting most recent row inside join


I have 2 tables companies and invoices
I want to select all companies with their most recent invoice price.
I don’t seem to get it working.
This is what I tried:

But the query loads very long and I don’t know why. The structure looks like this:

companies

invoices

The BC_ID is the same as the company_id and ISCOMMISSIE should be 0.
I want to select the most recent date.
Does someone have an idea on how to do this and also make the query as fast as possible? http://sqlfiddle.com/#!9/2fc3a/1

Advertisement

Answer

Try:

And the index invoices (ISCOMMISSIE, BC_ID, DATE) may help…

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement