1

I have 3 tables that are currently being joined using inner joins. The tables are:

invoice, contract and meter.

Some simplified sample data:

//invoice

id | contract_id

1  | 123

//contract

id  | meter_id | supplier     | end_date

123 | 100      | British Gas  | 2013-12-20

456 | 100      | nPower       | 2014-03-03

//meter

id  | meter-id
1   | 100

My aim is to join the tables but retrieve only the latest (MAX) end_date and get the supplier. Normally this wouldn't be a problem, but I only have contract 123 to join on, not contract 456. As shown, they both share the same meter_id.

//Current query
SELECT
contract.supplier AS supplierName
FROM invoice
INNER JOIN contract ON contract.id=invoice.contract_id
INNER JOIN meter ON meter.id=contract.meter_id

How do I do this? Is it via a nested select or something?? Thanks

4
  • Your try should work, what change you want ? Commented Dec 17, 2013 at 13:21
  • In this case do you want to bring back the supplier details for 123 (which are not the latest, but which do match on invoice) or 456 (which is the latest but doesn't have a matching invoice)? Could you add the expected output? Commented Dec 17, 2013 at 13:34
  • @anewvision: my comment is not upto your problem but Did you try LEFT JOIN and saw the difference in result also? Commented Dec 17, 2013 at 13:48
  • @Sohail - why would a LEFT JOIN work? The invoice table does not contain the meter_id. Commented Dec 17, 2013 at 14:13

4 Answers 4

5
SELECT *
FROM
(
    SELECT meter_id, supplier, MAX(end_date) end_date
    FROM contract 
    GROUP BY meter_id, supplier
) a
JOIN contract c ON c.meter_id = a.meter_id AND a.end_date = c.end_date
JOIN meter m ON m.meter-id = c.meter_id
JOIN invoice i ON i.contract_id = c.id
Sign up to request clarification or add additional context in comments.

3 Comments

Also (assuming a GROUP BY on meter_id) you probably want to remove supplier from the sub query (it might not refer to the one for the max end_date), and just get it from the join to the contract table
Sorry my bad missed the group by... Should be fine now
All working, thanks to kevin for the code and @paparazzokid for the fiddle.
3

It should be like :

    SELECT contract.supplier AS supplierName
    FROM invoice
    INNER JOIN contract ON contract.id=invoice.contract_id
    INNER JOIN meter ON meter.meter_id=contract.meter_id
    order by end_date DESC 
    limit 1

1 Comment

That would work for the example, but not in a database full of thousands of records. I simplified the dataset for the question.
0

Hope this is what you want & this should work.

SELECT
contract.supplier AS 'supplierName'
end_date AS 'end_date'
FROM contract
INNER JOIN invoice ON contract.id=invoice.contract_id
INNER JOIN meter ON meter.id=contract.meter_id
WHERE end_date in (select MAX(end_date) from contract)

If you want it to be latest (MAX) end_date for each supplier, simply change the sub-query & add group by supplier.

1 Comment

you can also alias, end_date as start_date, it doesn't matter. It is not compulsory to alias every column, but I did, anyways, thanks for vote down.
0
select i.contract_id,m.meter_id,c.supplier,c.end_date
from invoice i
inner join contract c on i.contract_id=c.id
inner join meter m c.meter_id=m.meter_id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.