MySQL Forums
Forum List  »  Newbie

Re: Three Table Join
Posted by: Peter Brawley
Date: January 14, 2018 02:39PM

> the situation I described is deliberately an example, as the real data is fairly confidential.

If you don't have a non-production copy of the database with test data in it, perhaps you now understand one reason you need it.

I asked for Show Create table results and Insert statements, rather than the informal descriptions you've posted, because with SQL in hand, I can investigate the problem without having to spend half an hour turning your text into usable SQL, and I can test any query I then write. So if you want such help, please provide Show Create Table results and enough Inserts needed to reproduce the problem. Meanwhile I can just suggest something in the neighbourhood of what you want ...

select c.customername, ifnull(x.week,'None') as week, ifnull(x.amt,0) as amtsum
from customers c
left join (
  select s.customerID, w.week, s.sales s 
  from sales s
  left join week w using(customerID,week)
  group by customerID, week
) x on c.customerID=x.customer.ID
order by c.customername

... the idea being that sales aggregation by week & customerID requires just the week and sales tables, to which the customers table needs to be joined to retrieve customer names for the aggregate results. Without your tables, though, I can't test it.

Options: ReplyQuote


Subject
Written By
Posted
January 11, 2018 08:37PM
January 11, 2018 11:01PM
January 14, 2018 01:38PM
Re: Three Table Join
January 14, 2018 02:39PM
January 14, 2018 03:47PM
January 14, 2018 04:34PM
January 14, 2018 09:18PM
January 14, 2018 08:52PM
January 14, 2018 10:25PM
January 14, 2018 09:29PM
January 14, 2018 09:36PM


Sorry, you can't reply to this topic. It has been closed.

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.