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 7817981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:25:06+00:00 2026-06-02T06:25:06+00:00

Now I have 3 tables Orders: ID | Date | Machine | Planning GoodPieces:

  • 0

Now I have 3 tables

Orders:

ID | Date | Machine | Planning

GoodPieces:

ID | Date | Machine | Pieces | Product

BadPieces:

ID | Date | Machine | Pieces | Product | Component

The output of the query is

Date | Machine | Planning | GoodPieces | BadPieces

The actual query is something like that:

SELECT
  data.Date AS Date,
  data.Machine AS Machine,
  SUM(CASE WHEN data.type = 'Planning' THEN data.value END ) AS Planning,
  SUM(CASE WHEN data.type = 'GoodPieces' THEN data.value END ) AS GoodPieces,
  SUM(CASE WHEN data.type = 'BadPieces' THEN data.value END ) AS BadPieces
FROM
      ( SELECT
          'Planning' AS Type,
          Date AS Date,
          Machine AS Machine,
          Planning AS Value
        FROM Orders
        UNION ALL
        SELECT
          'GoodPieces',
          Date,
          Machine,
          Pieces AS Value
        FROM GoodPieces
        UNION ALL
        SELECT
          'BadPieces'
          Date,
          Machine,
          Pieces AS Value
        FROM BadPieces ) AS data
GROUP BY
   Date,
   Machine

My question is if is there a way to get the same output with this 3 tables

Orders:

ID | Date | Machine| Planning

GoodPieces:

OrderID | Pieces | Product

BadPieces:

OrderID | Pieces | Product | Component

Thanks,

  • 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-02T06:25:08+00:00Added an answer on June 2, 2026 at 6:25 am

    Sure, just LEFT JOIN in the tables and sum the appropriate columns:

    SELECT
      O.Date,
      O.Machine,
      SUM(COALESCE(O.Planning, 0)) AS Planning,
      SUM(COALESCE(G.Pieces, 0)) AS GoodPieces,
      SUM(COALESCE(B.Pieces, 0)) AS BadPieces
    FROM
      Orders O
    LEFT JOIN
      (SELECT G.OrderID,
         SUM(COALESCE(G.Pieces, 0)) AS GoodPieces
       FROM GoodPieces G
       GROUP BY G.OrderID) G ON G.OrderID = O.ID
    LEFT JOIN
      (SELECT B.OrderID,
         SUM(COALESCE(B.Pieces, 0)) AS BadPieces
       FROM BadPieces B
       GROUP BY B.OrderID) B ON B.OrderID = O.ID
    GROUP BY
      O.Date,
      O.Machine;
    

    Demo: http://www.sqlfiddle.com/#!3/09a73/17

    Edit: Updated to handle point given by @MikaelEriksson in comments.

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

Sidebar

Related Questions

Hie frnds, I have two tables say Orders and OrdersXML now I wish to
I have two tables users and work_orders. Now I need to get work_orders results
I have two tables: This is table1: product_id|product_desc|product_name|product_s_desc This is table2: product_price_id|product_id|product_price Now I
I have two tables, one called cart and one called items. Now I want
I have 3 tables called radio, song, and artist controlled by favorite. Now I
I have 4 tables named wheels , tires , oil_change , other_servicing . Now,
i have two joined tables. Now i need to print out some columns, but
I have three MySQL InnoDB tables: Debtors Companies Private individuals Now I would like
I have the following tables: --table sportactivity-- sport_activity_id, home_team_fk, away_team_fk, competition_id_fk, date, time (tuple
I have 2 tables A and B with following records --TABLE A-- id date

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.