MySQL Forums
Forum List  »  Optimizer & Parser

Re: left join with sum and group by
Posted by: Rick James
Date: August 20, 2010 11:40PM

Give this a try... First find all the employees (SELECT DISTINCT ...). Then use a cross join to get all dates and all employees. Finally do the LEFT JOIN as you tried.

SELECT a.date, a.weekday, a.workinghours,
       e.employee,
       sum(b.hours),
       sum(b.hours) - a.workinghours
    FROM a
    JOIN ( SELECT DISTINCT employee FROM b ) AS e
    LEFT JOIN b  ON a.date = b.date
    GROUP BY  a.date, e.employee
(Note the missing ON clause in the JOIN)

Options: ReplyQuote


Subject
Views
Written By
Posted
24000
August 18, 2010 05:46AM
Re: left join with sum and group by
8384
August 20, 2010 11:40PM
6619
August 27, 2010 08:01PM
4042
September 07, 2010 05:08AM


Sorry, you can't reply to this topic. It has been closed.
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.