Solved

MYSQL: How to get different columns from different tables using one select statement

Posted on 2014-10-01
8
273 Views
Last Modified: 2014-10-02
I have two very similar tables (with identical column names), that I want to get results from using a single query.

My SQL is this

SELECT mbtd_yield_classic_round1.yldGrowerID, Sum(mbtd_yield_classic_round1.yldYield) AS SumYield1, Sum(mbtd_yield_classic_round2.yldYield) AS SumYield2
FROM mbtd_yield_classic_round1, mbtd_yield_classic_round2
GROUP BY mbtd_yield_classic_round1.yldGrowerID;

Open in new window


However as I am summing the Yield columns, the value returned are not what I expect. They should be:
A : 5.1 : 3.2
B : 3.7 : 4.0

instead I get

A : 51 : 36
A : 37 : 36

Can someone help me please to get the correct results from this simple query ?
0
Comment
Question by:Sean Clarke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40354931
Can you please provide some sample data?
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 40355484
Hi!

You will need to summarize one table at a time and then combine those results into one
like this

SELECT t1.yldGrowerID, Sum(t1.yldYield) AS SumYield
FROM mbtd_yield_classic_round1 t1
GROUP BY t1.yldGrowerID;
UNION
SELECT t2.yldGrowerID, Sum(t2.yldYield) AS SumYield
FROM mbtd_yield_classic_round2 t2 
GROUP BY t2.yldGrowerID;

Open in new window


Regards,
     Tomas Helgi
0
 
LVL 1

Author Comment

by:Sean Clarke
ID: 40356339
Hi, Sorry - I forgot to post my sample data.

Tomas, this UNION will only give me One Grower ID and ONE TOTAL SUM.
I Need two Totals - one from Table 1 and one from Table 2.
Table-1.PNG
Table-2.PNG
0
Webinar: MariaDB® Server 10.2: The Complete Guide

Join Percona’s Chief Evangelist, Colin Charles as he presents MariaDB Server 10.2: The Complete Guide on Tuesday, June 27, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40356355
Remove the GROUP By and the SUM and the problem will become apparent: you're using a Cartesian product, which means the resulting table will have 10X10 rows.

Try something like this:
SELECT t3.yldGrowerID, SUM(SumYield1), SUM(SumYield2)
FROM
(SELECT t1.yldGrowerID, Sum(t1.yldYield) AS SumYield1, '0' as SumYield2
FROM mtbd_yield_classic_round1 t1
GROUP BY t1.yldGrowerID
UNION
SELECT t2.yldGrowerID, '0' AS SumYield1, Sum(t2.yldYield) AS SumYield2
FROM mtbd_yield_classic_round2 t2 
GROUP BY t2.yldGrowerID) t3
GROUP BY t3.yldGrowerID

Open in new window

0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 40356444
Hi!

Then if you use UNION ALL like this
SELECT t1.yldGrowerID, Sum(t1.yldYield) AS SumYield
FROM mbtd_yield_classic_round1 t1
GROUP BY t1.yldGrowerID;
UNION ALL
SELECT t2.yldGrowerID, Sum(t2.yldYield) AS SumYield
FROM mbtd_yield_classic_round2 t2 
GROUP BY t2.yldGrowerID;

Open in new window


You should get 2 rows and if they are identical then the sum and values for yldGrowerID are the same in both
tables.

Regards,
    Tomas Helgi
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40356480
@Tomas: the OP wants the result to have 2 rows and 3 columns:

yldGrowerID  |  SumYield1  | SumYield2
     A                 |         5.1        |     3.2
     B                 |         3.7        |     4.0

Your query returns 4 rows and 2 columns.
0
 
LVL 1

Author Closing Comment

by:Sean Clarke
ID: 40356652
Excellent thank for this - kind of obvious when I look at what you've done, but would have never worked it out on my own.

My final query ended up like this:

SELECT t3.yldGrowerID, ROUND(SUM(t3.SumYield1),2) AS SumYield1, ROUND(SUM(t3.SumYield2),2) AS SumYield2 FROM
(SELECT t1.yldGrowerID, SUM(t1.yldYield) AS SumYield1, '0' AS SumYield2 FROM mbtd_yield_classic_round1 t1 GROUP BY t1.yldGrowerID
UNION
SELECT t2.yldGrowerID, '0' AS SumYield1, SUM(t2.yldYield) AS SumYield2 FROM mbtd_yield_classic_round2 t2 GROUP BY t2.yldGrowerID) t3 GROUP BY t3.yldGrowerID
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40356683
Glad I could help!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question