Simply put,
I have these tables.
Planning:
Date | Machine | Product | PlanningParts
OKParts:
Date | Machine | Product | OKParts
Scrap:
Date | Machine | Product | ScrapParts
Trials:
Date | Machine | Product | TrialParts
Breakdowns:
Date | Machine | Product | Minutes
These tables can have two or more records for a same [Date | Machine | Product], they are not unique columns.
What is the best SQL Code(SQLite or SQL Server) to get this output?
Output:
Date | Machine | Product | Planning | OKParts | ScrapParts | TrialParts | Minutes
Edit:
In the output, i need one line per [Date,Machine,Product] and the rest of the columns have to be the SUM
(I don’t speak english, Sorry)
Eidted:
Example:
(I would not put the table “Trials” to make it shorter)
Planning:
Date | Machine | Product | PlanningParts
1/6/12 | Blower | A001 | 100
2/6/12 | Blower | A002 | 100
2/6/12 | Assembly| B001 | 50
OKParts:
Date | Machine | Product | OKParts
1/6/12 | Blower | A001 | 50
1/6/12 | Blower | A001 | 20
1/6/12 | Blower | A002 | 100
Scrap:
Date | Machine | Product | ScrapParts | Reason
1/6/12 | Blower | A001 | 5 | Low Weight
1/6/12 | Blower | A001 | 3 | High Weight
2/6/12 | Assembly| B001 | 4 | Bad Cut
Breakdowns
Date | Machine | Product | Minutes | Reason
1/6/12 | Blower | A001 | 100 | Manteinance
1/6/12 | Blower | A001 | 20 | Manteinance
2/6/12 | Assembly| B001 | 100 | Quality approval
OUTPUT:
Date | Machine | Product | Planning | OKParts | ScrapParts | Breakdowns
1/6/12 | Blower | A001 | 100 | 70 | 8 | 120
1/6/12 | Blower | A002 | 100 | 100 | 0 | 0
2/6/12 | Assembly| B001 | 50 | 0 | 4 | 100
Create a CTE unioning all the tables into one, then using the SUM aggrigate function, add up the columns for each Date, Machine, Product group. A bit like this (not tested):