I have a stored procedure that runs a select statement that returns one row and inserts the contents into a temporary table. The select statement has 6 conditional statements in a where clause and I essentially need to loop through 3 additional sets of criteria and insert those results into the temp table. Here is what I have so far:
CREATE DEFINER=`sleuser`@`%` PROCEDURE `CostDashboard`()
BEGIN
create temporary table TempTable (
ProjectID int,
Phase varchar(100),
OriginalCommitments float,
ApprovedCommitmentChanges float,
CurrentAssigned float,
PendingScopeChanges float,
EAC float,
PercentComplete float
);
insert into TempTable(
SELECT project_id,
'FP' as Phase,
OriginalCommitments,
ApprovedCommitmentChanges,
OriginalCommitments+ApprovedCommitmentChanges as CurrentAssigned,
sum(ProjectCostBudget.PendingChangeOrders) as PendingScopeChanges
sum(ProjectCost.CurrentWorkCompleted) +
sum(ProjectCost.EstimateToComplete) as EAC,
(sum(ProjectCost.CurrentWorkCompleted) +
sum(ProjectCost.EstimateToComplete) /
(sum(ProjectCostBudget.OriginalContractPrice +
ProjectCostBudget.RegisteredChangeOrders))) as PercentComplete
FROM `RCLY-DEV`.project
inner join ImportCost on ImportCost.ProjectID = project.pmis
inner join ProjectCostBudget on ProjectCostBudget.ProjectID =
project.project_id
inner join ProjectCost on ProjectCost.ProjectID = project.project_id
where ImportCost.ProjectID = 'RLCY-BB-01'
and ImportCost.Task = "020.0000.000"
and ProjectCostBudget.ProjectID = 2
and ProjectCostBudget.ServiceNumber = "020.0000.000"
and ProjectCost.MonthYear != ''
and ProjectCost.MonthYear like 'July%2018'
);
select * from TempTable
;
END
This works and inserts the one record with hard coded values in the where clause, but I need to run it for 3 sets of variables, so I created an additional temporary table like this:
|ImpCostID|ImpCostTask |PCBID|PCBServNum |MonthYear|
-----------------------------------------------------
|XXY-01-01|030.0000.000|3 |030.0000.000|July%2018|
|QWY-01-01|040.0000.000|4 |040.0000.000|May%2018 |
|ZXF-01-01|040.0000.000|5 |050.0000.000|June%2018|
but I'm not sure how to assign these sets of values to variables and then loop through them. Any suggestions?