|
-
Aug 26th, 2016, 08:16 AM
#1
[RESOLVED] join 3 tables?
Hey all,
On a MySQL server I have 3 tables. One table is the customers table (table C), and the other 2 tables (tables R & P) measure process timing 2 different ways. Each of these tables has a foreign key pointing back to the customers table and a "minutes" field.
What I need is an SQL statement that returns a table with the company, sum(Table R.minutes) and sum(Table P.minutes) for each customer.
This is a select statement that does run, but gives me the wrong values...
Code:
SELECT C.id,
C.company,
FORMAT(sum(R.minutes),2) as 'RM' ,
FORMAT(sum(P.minutes),2) as 'BM' FROM customers as C
JOIN p_table as P ON P.customerID=C.id
JOIN r_table as R ON R.customerID=C.id
GROUP BY C.company
ORDER BY C.id
Can anyone show me the correct sql to do this?
thanks
kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Aug 26th, 2016, 09:12 AM
#2
Re: join 3 tables?
Your grouping is wrong you need to GROUP BY C.id and C.company.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Aug 26th, 2016, 09:30 AM
#3
Re: join 3 tables?
Thanks for the feedback, but adding C.ID doesn't change the resulting table unfortunately. Gawd I wish I knew this stuff better. I feel another "learning experience" coming.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Aug 26th, 2016, 10:08 AM
#4
Re: join 3 tables?
Hard to say w/o knowing what you are getting vs what you think you should be getting...
-tg
-
Aug 26th, 2016, 10:20 AM
#5
Re: join 3 tables?
it's a bit of a hack try this:
SQL Code:
SELECT R.id, ,R.company ,R.RM ,P.BM FROM (SELECT C.id, C.company, FORMAT(sum(R.minutes),2) as 'RM' , FROM customers as C JOIN r_table as R ON R.customerID=C.id GROUP BY C.id,C.company) AS R LEFT OUTER JOIN (SELECT C.id, C.company, FORMAT(sum(P.minutes),2) as 'BM' FROM customers as C JOIN p_table as P ON P.customerID=C.id GROUP BY C.id, C.company) As P ON R.id = P.id
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Aug 26th, 2016, 10:24 AM
#6
Re: join 3 tables?
 Originally Posted by tg
Hard to say w/o knowing what you are getting vs what you think you should be getting...
yea fair enough...
My C table...
| id |
company |
| 1 |
msb |
| 2 |
mbDemo |
My PTable
| id |
customerID |
minutes |
| 1 |
1 |
10 |
| 2 |
2 |
20 |
| 3 |
2 |
21 |
| 4 |
1 |
11 |
My R Table
| id |
customerID |
minutes |
| 1 |
2 |
2 |
| 2 |
1 |
1 |
| 3 |
2 |
2 |
| 4 |
1 |
1 |
Running this query...
Code:
select C.id,
C.company,
FORMAT(sum(R.minutes),2) as 'RM' ,
FORMAT(sum(P.minutes),2) as 'PM' from customers as C
join production_table as P on P.customerID=C.id
join runtimetable as R on R.customerID=C.id
Group by C.id,C.company
order by C.id
Yields this result...
| id |
company |
RM |
PM |
| 1 |
mbs |
4.00 |
42.00 |
| 2 |
mbDemo |
8.00 |
82.00 |
yet I need it be be
| id |
company |
RM |
PM |
| 1 |
mbs |
2.00 |
21.00 |
| 2 |
mbDemo |
4.00 |
41.00 |
so that RM and PM are the sums of minutes for each company.
Last edited by kebo; Aug 26th, 2016 at 10:36 AM.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Aug 26th, 2016, 10:25 AM
#7
Re: join 3 tables?
Actually... the left join rather than the default inner join should work.
Code:
SELECT C.id,
C.company,
FORMAT(sum(R.minutes),2) as 'RM' ,
FORMAT(sum(P.minutes),2) as 'BM'
FROM customers as C
LEFT JOIN p_table as P ON P.customerID=C.id
LEFT JOIN r_table as R ON R.customerID=C.id
GROUP BY C.company
ORDER BY C.id
-tg
edit -we cross posted... AH... yes, your'e getting a cartesian effect there....
I think a variation on Gary's code would work best -- although, his would work, it's not quite how I'd do it... probably just a style call but....
Code:
SELECT
C.id,
,C.company
,R.RM
,P.BM
FROM Customer C
LEFT OUTER JOIN
(SELECT
CustomerID,
FORMAT(sum(minutes),2) AS 'RM' ,
FROM r_table
GROUP BY customerID) R
on C.ID = R.CustomerID
LEFT OUTER JOIN
(SELECT CustomerID,
FORMAT(sum(minutes),2) AS 'BM'
FROM p_table
GROUP BY CustomerID) P
ON C.id = P.Customerid
-tg
Last edited by techgnome; Aug 26th, 2016 at 10:30 AM.
-
Aug 26th, 2016, 10:32 AM
#8
Re: join 3 tables?
 Originally Posted by techgnome
Actually... the left join rather than the default inner join should work.
Code:
SELECT C.id,
C.company,
FORMAT(sum(R.minutes),2) as 'RM' ,
FORMAT(sum(P.minutes),2) as 'BM'
FROM customers as C
LEFT JOIN p_table as P ON P.customerID=C.id
LEFT JOIN r_table as R ON R.customerID=C.id
GROUP BY C.company
ORDER BY C.id
-tg
That unfortunately yields the same result. Essentially all of the result values are double.
This on the other hand.....
 Originally Posted by GaryMazzone
it's a bit of a hack try this:
SQL Code:
- SELECT
- R.id,
- ,R.company
- ,R.RM
- ,P.BM
- FROM
- (SELECT
- C.id,
- C.company,
- FORMAT(sum(R.minutes),2) AS 'RM' ,
- FROM customers AS C
- JOIN r_table AS R ON R.customerID=C.id
- GROUP BY C.id,C.company) AS R
- LEFT OUTER JOIN
- (SELECT C.id,
- C.company,
- FORMAT(sum(P.minutes),2) AS 'BM'
- FROM customers AS C
- JOIN p_table AS P ON P.customerID=C.id
- GROUP BY C.id, C.company) AS P
- ON R.id = P.id
Does work with a little syntax correcting.
I knew this would be a learning experience. I'll spend the next 10-15 figuring out why it work only to get to learn it again in 6 months
Thanks to both
kevin
Here is the final sql that runs....
Code:
SELECT
R.id, R.company, R.RM, P.BM
FROM
(SELECT
C.id, C.company, FORMAT(sum(R.minutes),2) AS 'RM'
FROM customers AS C
JOIN r_table AS R ON R.customerID=C.id
GROUP BY C.id,C.company) AS R
LEFT OUTER JOIN
(SELECT C.id, C.company, FORMAT(sum(P.minutes),2) AS 'BM'
FROM customers AS C
JOIN p_table AS P ON P.customerID=C.id
GROUP BY C.id, C.company) AS P
ON R.id = P.id
Last edited by kebo; Aug 26th, 2016 at 10:35 AM.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Aug 26th, 2016, 10:56 AM
#9
Re: join 3 tables?
What I did was to create 2 derived tables on for each of the actuals. then joined them together for a finial output. The other (orginal) could work I just don't have the time to recreate all the tables and work it out. I don't use MySQL so it is based generic SQL Ansi code
Sometimes the Programmer
Sometimes the DBA
Mazz1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|