Subject | SubQuery Help |
---|---|
Author | |
Post date | 2014-09-16T16:02:18Z |
I have a master table and two detail tables. The Master holds Projects, with a primary key. One detail table holds estimated and actual costs in separate columns. The other hold estimated and actual quantities of materials in separate columns. It is a one to many situation, with one project have many cost and quantities.
I would like to prepare a query that will sum each of the the four items as four columns (estimated costs, actual costs, estimated quantities, and actual quantities) by the Master table's primary key (the Master tables primary keys are contained in the details tables as well).
I am thinking that it could be something like:
Select
Project_PK,
(select sum(cost) from Costs where project_pk=project.project_pk) as Sum_of_Cost,
(select sum(estimate_cost)from Costs project_pk=project.project_pk) as Sum_of_Estimated_Cost,
(select sum(quantity) from Quanties project_pk=project.project_pk) as Sum_of_Quantity,
(select sum(estimate_Quantity) from Quantity project_pk=project.project_pk) as Sum_of_Estimated_Quantity,
from Projects
Am I on the right track? One complication is that not all Projects have Costs or Quantities, yet I need to have all the projects in the result.
Thanks
I would like to prepare a query that will sum each of the the four items as four columns (estimated costs, actual costs, estimated quantities, and actual quantities) by the Master table's primary key (the Master tables primary keys are contained in the details tables as well).
I am thinking that it could be something like:
Select
Project_PK,
(select sum(cost) from Costs where project_pk=project.project_pk) as Sum_of_Cost,
(select sum(estimate_cost)from Costs project_pk=project.project_pk) as Sum_of_Estimated_Cost,
(select sum(quantity) from Quanties project_pk=project.project_pk) as Sum_of_Quantity,
(select sum(estimate_Quantity) from Quantity project_pk=project.project_pk) as Sum_of_Estimated_Quantity,
from Projects
Am I on the right track? One complication is that not all Projects have Costs or Quantities, yet I need to have all the projects in the result.
Thanks