Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:10:48+00:00 2026-06-11T15:10:48+00:00

MySQL is still pretty new to me, so I’m having difficutly finding the answer

  • 0

MySQL is still pretty new to me, so I’m having difficutly finding the answer to this particular problem.

I have roughly 60 vendor tables that I would like to join into one table. Alot of our vendors carry the same items so I would like to add all of the items to one table, and those that have the same sku, add the quantities together. I would also like to insert the greater cost and price, and round it up to .99, but if it gets too complicated or can’t be sone, I will stick with just the sku’s and quanitity for now. I would like to start with 2 tables until I can figure it out, then once I grasp the solution I can work on all of them. Here’s an example of 2 tables:

Table A (SKU will be the primary key)
SKU     Vendor A SKU         Cost     Price      QTY
1000        VA100            49.99    62.99      10
1001        VA101            64.44    81.99      6
1002        VA102            37.67    47.99      24
1003        VA103            28.33    35.99      16
1004        VA104            26.33    35.99      18

Table B (SKU will be the primary key)

SKU     Vendor B SKU         Cost    Price       QTY
1000        VB100            49.99   62.95        21
1001        VB101            64.44   81.95        16
5001        VB501            32.67   40.95         4
5002        VB502            88.44   110.95      12
5003        VB503            87.21   109.95       2

So the End Result will look like this

SKU     Vendor A SKU     Vendor B SKU      Cost           Price         QTY
1000        VA100               VB100      49.99         62.99          31 <-- Sum
1001        VA101               VB101      64.44         81.99          22 <-- Sum
1002        VA102                          37.67         47.99          24
1003        VA103                          28.33         35.99          16
1004        VA104                          26.33         35.99          18
5001                            VB501      32.67         40.95           4
5002                            VB502      88.44         110.95         12
5003                            VB503      87.21         109.95          2

As you can see the first 2 had the quantities added together, as well as the greater of cost and price inserted. I’ve tried variations of:

Create Table MASTER
SELECT TableA.sku,TableA.VendorA_sku,TableA.cost,TableA.price,TableA.qty
UNION ALL
SELECT TableB.sku,TableB.VendorB_sku,TableB.cost,TableB.price,TableB.qty
GROUP BY sku

And variations of:

Create Table MASTER
AS SELECT TableA.sku,TableA.VendorA_sku,TableA.cost,TableA.price,SUM(TableA.qty+TableB.qty)
GROUP BY sku

I’ve tried numerous different types of Joins and unions and can’t seem to get anything to look the way I need it to, nor have I played around with the adding and rounding of cost and price, as I can’t get past the adding of quanitity in duplicate Primary keys. I have to do this on a daily basis with roughly 60 tables and about 600k sku’s, so any help would be greatly appreciated.

(I also Apologize in advance if I didn’t type this correctly as I’m new to these types of forums)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:10:49+00:00Added an answer on June 11, 2026 at 3:10 pm

    The following should work for you:

    select sku,
      min(case when Type = 'VendorA' THEN Vendor END) VendorA_SKU,
      min(case when Type = 'VendorB' THEN Vendor END) VendorB_SKU,
      cost,  
      price, 
      sum(QTY) Total
    from
    (
      select sku, VendorA_SKU Vendor, cost, price, qty, 'VendorA' Type
      from tablea
      union all 
      select sku, VendorB_SKU, cost, price, qty, 'VendorB' Type
      from tableb
    ) x
    group by sku
    

    see SQL Fiddle with Demo

    A few things to decide, do you want the AVG() or MAX() cost and price if the values are different or which one do you want to return? If you do then you will use the following to return MAX():

    select sku,
      min(case when Type = 'VendorA' THEN Vendor END) VendorA_SKU,
      min(case when Type = 'VendorB' THEN Vendor END) VendorB_SKU,
      max(cost) MaxCost,
      max(price) MaxPrice,
      sum(QTY) Total
    from
    (
      select sku, VendorA_SKU Vendor, cost, price, qty, 'VendorA' Type
      from tablea
      union all 
      select sku, VendorB_SKU, cost, price, qty, 'VendorB' Type
      from tableb
    ) x
    group by sku
    

    see SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still pretty new with MySQL and I don't know the best way to
I am pretty new to both php and mysql. I have a txt file
I'm still pretty new to php and mysql, I've build a basic CMS for
I'm pretty sure this particular quirk isn't a duplicate so here goes. I have
This is pretty specific and I have not found an answer yet. I am
I'm still fairly new to mysql and would like to ask for some help.
I have been using MySQL for 2 years now, yet I still don't know
I have a large database in MySQL and it is about 4 Gb, still
I have made a registration program. Making use of mysql database. Can I still
I am pretty new to hibernate and I am stuck at this exception. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.