Hi there ive been trying to figure this out but not to many examples available,
i have the following procedure:
my question would be:
if i want to add to new columns from 2 different new table how would it influence my union all select statements?
??/?
"}{
ALTER procedure [dbo].[sp_DailyProduction_DataNew]
@ProdMonth varchar(6)
as
declare @ProdMonth varchar(6)
set @ProdMonth = '201304'
select
ProdMonth,
SectionID_2,
Name_2,
max(SectionID) SectionID,
max(Name_1) Name_1,
WPID,
Description,
CalendarDate,
ShiftNo,
max(Activity) Activity,
max(Rigg) Rigg,
max(CostAccount) CostAccount,
sum(OpsPlan_Metres) OpsPlan_Metres,
sum(OpsPlan_Tons) OpsPlan_Tons,
sum(OpsPlan_GT) OpsPlan_GT,
sum(OpsPlan_Kg) OpsPlan_Kg,
sum(PlanDynMetres) PlanDynMetres,
sum(PlanDynGT) PlanDynGT,
sum(PlanDynVolume) PlanDynVolume,
sum(PlanDynKG) PlanDynKG,
sum(PlanDynDesSqm) PlanDynDesSqm,
sum(PlanLockMetres) PlanLockMetres,
sum(PlanLockGT) PlanLockGT,
sum(PlanLockVolume) PlanLockVolume,
sum(PlanLockKG) PlanLockKG,
sum(PlanLockDesSqm) PlanLockDesSqm,
sum(PlanProgAdv) PlanProgAdv,
sum(PlanDayAdv) PlanDayAdv,
sum(PlanDayReefMetres) PlanDayReefMetres,
sum(PlanDayWasteMetres) PlanDayWasteMetres,
sum(BookProgAdv) BookProgAdv,
sum(BookDayAdv) BookDayAdv,
max(EndHeight) EndHeight,
max(EndWidth) EndWidth,
sum(PlanProgVolume) PlanProgVolume,
sum(PlanDayVolume) PlanDayVolume,
sum(BookProgVolume) BookProgVolume,
sum(BookDayVolume) BookDayVolume,
sum(BookDayDesSqm) BookDayDesSqm,
sum(BookDayTons) BookDayTons,
sum(BookProgTons) BookProgTons,
sum(BookDayKg) BookDayKg,
sum(BookProgKg) BookProgKg,
sum(BookGT) BookGT,
sum(PlanProgTons) PlanProgTons,
sum(PlanDayTons) PlanDayTons,
sum(PlanDayReefTons) PlanDayReefTons,
sum(PlanDayWasteTons) PlanDayWasteTons,
sum(PlanGT) PlanGT,
sum(PlanProgKG) PlanProgKG,
sum(PlanDayKG) PlanDayKG,
max(LargeEnd) LargeEnd,
max(SmallEnd) SmallEnd,
sum(SimbaDay) SimbaDay,
sum(TramDayPlan) TramDayPlan,
sum(TramProgPlan) TramProgPlan,
sum(TramDayBook) TramDayBook,
sum(TramProgBook) TramProgBook,
sum(BookSimbaMetresDrilled) BookSimbaMetresDrilled,
sum(BookSimbaMetresFC) BookSimbaMetresFC,
sum(LargeEndsFC) LargeEndsFC,
sum(SmallEndFC) SmallEndFC,
sum(TonsLoadedLH) TonsLoadedLH,
sum(BackfillPlanDaily) BackfillPlanDaily,
sum(BackfillPlanProg) BackfillPlanProg,
sum(BackfillBookDaily) BackfillBookDaily,
sum(BackfillBookProg) BackfillBookProg,
max(CostingActivity) CostingActivity,
max(Critical) Critical
from
(
--declare @ProdMonth varchar(6)
--set @ProdMonth = '201304'
select shft.ProdMonth, shft.SectionID_2, shft.Name_2 ,
g.SectionID SectionID, g.Name Name_1,
p.WPID, w.Description, shft.ShiftNo,
shft.CalendarDate,
Activity = max(ca.Description),
Rigg = max(Rigg),
CostAccount = max(case when isnull(w.Account,'') = 'W' then 'Working'
when isnull(w.Account,'') = 'C' then 'Capital' else 'Unknown' end),
OpsPlan_Metres = sum(0),
OpsPlan_Tons = sum(0),
OpsPlan_GT = sum(0),
OpsPlan_Kg = sum(0),
PlanDynMetres = max(isnull(p.MetersAdavance,0)),
PlanDynGT = max(isnull(p.[g/t],0)),
PlanDynVolume = max(isnull(p.[Volume(m3)],0)),
PlanDynKG = max(isnull(p.Kg,0)),
PlanDynDesSqm = max(case when w.MiningTec = 2 then isnull(p.MetersAdavance,0) * isnull(p.EndWidth,0) else 0 end),
PlanLockMetres = sum(0),
PlanLockGT = sum(0),
PlanLockVolume = sum(0),
PlanLockKG = sum(0),
PlanLockDesSqm = sum(0),
PlanProgAdv = sum(case when pd.PlanDate <= shft.CalendarDate and pd.PlanDate >= shft.BeginDate
then isnull(pd.MetresAdvance,0) else 0 end),
PlanDayAdv = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.MetresAdvance,0) else 0 end),
PlanDayReefMetres = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.OnMetresAdvance,0) else 0 end),
PlanDayWasteMetres = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.OffMetresAdvance,0) else 0 end),
EndHeight = max(isnull(p.EndHeight,0)),
EndWidth = max(isnull(p.EndWidth,0)),
PlanProgVolume = sum(case when pd.PlanDate <= shft.CalendarDate and pd.PlanDate >= shft.BeginDate
then isnull(pd.Volume,0) else 0 end),
PlanDayVolume = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.Volume,0) else 0 end),
PlanProgTons = sum(case when pd.PlanDate <= shft.CalendarDate and pd.PlanDate >= shft.BeginDate
then isnull(pd.Tons,0) else 0 end),
PlanDayTons = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.Tons,0) else 0 end),
PlanDayReefTons = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.OnTons,0) else 0 end),
PlanDayWasteTons = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.OffTons,0) else 0 end),
PlanGT = sum(0),
PlanProgKG = sum(case when pd.PlanDate <= shft.CalendarDate and pd.PlanDate >= shft.BeginDate
then isnull(pd.Kg,0) else 0 end),
PlanDayKG = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.Kg,0) else 0 end),
BookDayAdv = sum(case when b.CalendarDate = shft.CalendarDate then
isnull(b.LargeEndsMetresBook,0) + isnull(b.SmallEndsMetresBook,0) else 0 end),
BookProgAdv = sum(case when b.CalendarDate <= shft.CalendarDate and b.CalendarDate >= shft.BeginDate then
isnull(b.LargeEndsMetresBook,0) + isnull(b.SmallEndsMetresBook,0) else 0 end),
BookDayVolume = sum(case when b.CalendarDate = shft.CalendarDate then
isnull(b.Cubics,0) else 0 end),
BookProgVolume = sum(case when b.CalendarDate <= shft.CalendarDate and b.CalendarDate >= shft.BeginDate then
isnull(b.Cubics,0) else 0 end),
BookDayDesSqm = sum(case when b.CalendarDate = shft.CalendarDate then
(isnull(b.LargeEndsMetresBook,0) + isnull(b.SmallEndsMetresBook,0)) * isnull(b.EndWidth,0) else 0 end),
BookDayTons = sum(case when b.CalendarDate = shft.CalendarDate then
isnull(b.Tons,0) else 0 end),
BookProgTons = sum(case when b.CalendarDate <= shft.CalendarDate and b.CalendarDate >= shft.BeginDate then
isnull(b.Tons,0) else 0 end),
BookDayKg = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.Kg,0) else 0 end),
BookProgKg = sum(case when b.CalendarDate <= shft.CalendarDate and b.CalendarDate >= shft.BeginDate then
isnull(b.Kg,0) else 0 end),
BookGT = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.GT,0) else 0 end),
SimbaDay = sum(0),
TramDayPlan = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.Tons,0) else 0 end),
TramProgPlan = sum(case when pd.PlanDate <= shft.CalendarDate and pd.PlanDate >= shft.BeginDate
then isnull(pd.Tons,0) else 0 end),
TramDayBook = sum(0),
TramProgBook = sum(0),
BookSimbaMetresDrilled = sum(0),
BookSimbaMetresFC = sum(0),
LargeEndsFC = sum(0),
SmallEndFC = sum(0),
TonsLoadedLH = max(case when bl.CalendarDate = shft.CalendarDate then isnull(bl.LongholeTonsLoaded,0) else 0 end),
BackfillPlanDaily = sum(case when pd.PlanDate = shft.CalendarDate then isnull(pd.Volume,0) else 0 end),
BackfillPlanProg = sum(case when pd.PlanDate <= shft.CalendarDate and pd.PlanDate >= shft.BeginDate
then isnull(pd.Volume,0) else 0 end),
BackfillBookDaily = sum(0),
BackfillBookProg = sum(0),
CostingActivity = max(isnull(c.CostingActivity,'')),
LargeEnd = max(case when w.CostingActivityID in (1,8) and w.LargeEnd = 'L' then 'Yes' else '' end),
SmallEnd = max(case when w.CostingActivityID in (1,8) and w.LargeEnd = 'S' then 'Yes' else '' end),
Critical = Max(case when w.Critical = 'Y' then 'Yes' else '' end)
from vw_Shifts shft
right join Planning p on
p.ProdMonth = shft.ProdMonth and
p.ProjectID = shft.SectionID
left outer join Planday pd on
p.ProdMonth = pd.ProdMonth and
p.ProjectID = pd.OrgID and
p.WPID = pd.WorkPlaceID and
p.PlanType = pd.PlanType and
pd.PlanDate >= shft.BeginDate and
pd.PlanDate <= shft.CalendarDate
left outer join BookingWP b on
b.SectionID = p.ProjectID and
b.WorkplaceID = p.WPID and
b.Activity = p.PlanType and
b.CalendarDate >= shft.BeginDate and
b.CalendarDate <= shft.CalendarDate and
b.TypeWP <> 'LH'
left outer join BookingWP bl on
bl.SectionID = p.ProjectID and
bl.WorkplaceID = p.WPID and
bl.Activity = p.PlanType and
bl.CalendarDate = shft.CalendarDate and
bl.TypeWP = 'LH'
inner join Workplace w on
w.WorkplaceID = p.WPID
left outer join Code_CostingActivity c on
c.CostingActivityID = w.CostingActivityID
left outer join CODE_ACTIVITY ca on
ca.Activity = p.PlanType
left join vw_Geo_Complete g on
w.OreFlowID = g.SectionID
where shft.ProdMonth = @ProdMonth
group by shft.ProdMonth, shft.SectionID_2, shft.Name_2,
g.SectionID, g.Name,
p.WPID, w.Description,
shft.CalendarDate, shft.ShiftNo
union All
--declare @ProdMonth varchar(6)
--set @ProdMonth = '201304'
select shft.ProdMonth, shft.SectionID_2, shft.Name_2,
g.SectionID SectionID, g.Name Name_1,
p.WPID, w.Description, shft.ShiftNo,
shft.CalendarDate,
Activity = max(''),
Rigg = max(''),
CostAccount = max(''),
OpsPlan_Metres = sum(0),
OpsPlan_Tons = sum(0),
OpsPlan_GT = sum(0),
OpsPlan_Kg = sum(0),
PlanDynMetres = sum(0),
PlanDynGT = sum(0),
PlanDynVolume = sum(0),
PlanDynKG = sum(0),
PlanDynDesSqm = sum(0),
PlanLockMetres = sum(isnull(p.MetersAdavance,0)),
PlanLockGT = sum(isnull(p.[g/t],0)),
PlanLockVolume = sum(isnull(p.[Volume(m3)],0)),
PlanLockKG = sum(isnull(p.Kg,0)),
PlanLockDesSqm = sum(case when w.MiningTec = 2 then isnull(p.MetersAdavance,0) * isnull(p.EndWidth,0) else 0 end),
PlanProgAdv = sum(0),
PlanDayAdv = sum(0),
PlanDayReefMetres = sum(0),
PlanDayWasteMetres = sum(0),
EndHeight = sum(0),
EndWidth = sum(0),
PlanProgVolume = sum(0),
PlanDayVolume = sum(0),
PlanProgTons = sum(0),
PlanDayTons = sum(0),
PlanDayReefTons = sum(0),
PlanDayWasteTons = sum(0),
PlanGT = sum(0),
PlanProgKG = sum(0),
PlanDayKG = sum(0),
BookDayAdv = sum(0),
BookProgAdv = sum(0),
BookDayVolume = sum(0),
BookProgVolume = sum(0),
BookDayDesSqm = sum(0),
BookDayTons = sum(0),
BookProgTons = sum(0),
BookDayKg = sum(0),
BookProgKg = sum(0),
BookGT = sum(0),
SimbaDay = sum(0),
TramDayPlan = sum(0),
TramProgPlan = sum(0),
TramDayBook = sum(0),
TramProgBook = sum(0),
BookSimbaMetresDrilled = sum(0),
BookSimbaMetresFC = sum(0),
LargeEndsFC = sum(0),
SmallEndFC = sum(0),
TonsLoadedLH = sum(0),
BackfillPlanDaily = sum(0),
BackfillPlanProg = sum(0),
BackfillBookDaily = sum(0),
BackfillBookProg = sum(0),
CostingActivity = max(''),
LargeEnd = max(''),
SmallEnd = max(''),
Critical = max('')
from vw_Shifts shft
right join PlanningLocked p on
p.ProdMonth = shft.ProdMonth and
p.ProjectID = shft.SectionID
inner join Workplace w on
w.WorkplaceID = p.WPID
left join vw_Geo_Complete g on
w.OreFlowID = g.SectionID
where shft.ProdMonth = @ProdMonth
group by shft.ProdMonth, shft.SectionID_2, shft.Name_2,
g.SectionID, g.Name,
p.WPID, w.Description,
shft.CalendarDate, shft.ShiftNo
union All
--declare @ProdMonth varchar(6)
--set @ProdMonth = '201304'
select shft.ProdMonth, shft.SectionID_2, shft.Name_2 ,
g.SectionID SectionID, g.Name Name_1,
b.WorkplaceID WPID, w.Description, shft.ShiftNo,
shft.CalendarDate,
Activity = max(''),
Rigg = max(''),
CostAccount = max(''),
OpsPlan_Metres = sum(0),
OpsPlan_Tons = sum(0),
OpsPlan_GT = sum(0),
OpsPlan_Kg = sum(0),
PlanDynMetres = sum(0),
PlanDynGT = sum(0),
PlanDynVolume = sum(0),
PlanDynKG = sum(0),
PlanDynDesSqm = sum(0),
PlanLockMetres = sum(0),
PlanLockGT = sum(0),
PlanLockVolume = sum(0),
PlanLockKG = sum(0),
PlanLockDesSqm = sum(0),
PlanProgAdv = sum(0),
PlanDayAdv = sum(0),
PlanDayReefMetres = sum(0),
PlanDayWasteMetres = sum(0),
EndHeight = sum(0),
EndWidth = sum(0),
PlanProgTons = sum(0),
PlanDayTons = sum(0),
PlanDayReefTons = sum(0),
PlanDayWasteTons = sum(0),
PlanProgVolume = sum(0),
PlanDayVolume = sum(0),
PlanGT = sum(0),
PlanProgKG = sum(0),
PlanDayKG = sum(0),
BookDayAdv = sum(0),
BookProgAdv = sum(0),
BookDayVolume = sum(0),
BookProgVolume = sum(0),
BookDayDesSqm = sum(0),
BookDayTons = sum(0),
BookProgTons = sum(0),
BookDayKg = sum(0),
BookProgKg = sum(0),
BookGT = sum(0),
SimbaDay = sum(0),
TramDayPlan = sum(0),
TramProgPlan = sum(0),
TramDayBook = sum(0),
TramProgBook = sum(0),
BookSimbaMetresDrilled = sum(0),
BookSimbaMetresFC = sum(0),
LargeEndsFC = sum(0),
SmallEndFC = sum(0),
TonsLoadedLH = sum(0),
BackfillPlanDaily = sum(0),
BackfillPlanProg = sum(0),
BackfillBookDaily = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.Cubics,0) else 0 end),
BackfillBookProg = sum(case when b.CalendarDate <= shft.CalendarDate and b.CalendarDate >= shft.BeginDate then
isnull(b.Cubics,0) else 0 end),
CostingActivity = max(''),
LargeEnd = max(''),
SmallEnd = max(''),
Critical = max('')
from vw_Shifts shft
right join Backfill b on
b.SectionID = shft.SectionID and
b.CalendarDate <= shft.CalendarDate and
b.CalendarDate >= shft.BeginDate
inner join Workplace w on
w.WorkplaceID = b.WorkplaceID
left join vw_Geo_Complete g on
w.OreFlowID = g.SectionID
where shft.ProdMonth = @ProdMonth
group by shft.ProdMonth, shft.SectionID_2, shft.Name_2,
g.SectionID, g.Name,
b.WorkplaceID, w.Description,
shft.CalendarDate, shft.ShiftNo
union All
--declare @ProdMonth varchar(6)
--set @ProdMonth = '201304'
select ProdMonth = @ProdMonth, shft.SectionID_2, shft.Name_2 ,
isnull(d.SectionID,'') SectionID, isnull(d.Name_1,'') Name_1,
isnull(d.WPID,'') WorkPlaceID, isnull(d.Description,'') Description, shft.ShiftNo,
shft.CalendarDate,
Activity = max(''),
Rigg = max(''),
CostAccount = max(''),
OpsPlan_Metres = sum(0),
OpsPlan_Tons = sum(0),
OpsPlan_GT = sum(0),
OpsPlan_Kg = sum(0),
PlanDynMetres = sum(0),
PlanDynGT = sum(0),
PlanDynVolume = sum(0),
PlanDynKG = sum(0),
PlanDynDesSqm = sum(0),
PlanLockMetres = sum(0),
PlanLockGT = sum(0),
PlanLockVolume = sum(0),
PlanLockKG = sum(0),
PlanLockDesSqm = sum(0),
PlanProgAdv = sum(0),
PlanDayAdv = sum(0),
PlanDayReefMetres = sum(0),
PlanDayWasteMetres = sum(0),
EndHeight = sum(0),
EndWidth = sum(0),
PlanProgVolume = sum(0),
PlanDayVolume = sum(0),
PlanProgTons = sum(0),
PlanDayTons = sum(0),
PlanDayReefTons = sum(0),
PlanDayWasteTons = sum(0),
PlanGT = sum(0),
PlanProgKG = sum(0),
PlanDayKG = sum(0),
BookDayAdv = sum(0),
BookProgAdv = sum(0),
BookDayVolume = sum(0),
BookProgVolume = sum(0),
BookDayDesSqm = sum(0),
BookDayTons = sum(0),
BookProgTons = sum(0),
BookDayKg = sum(0),
BookProgKg = sum(0),
BookGT = sum(0),
SimbaDay = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.SimbaMetresDrilled,0) else 0 end),
TramDayPlan = sum(0),
TramProgPlan = sum(0),
TramDayBook = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.TrammedOutReefTons,0) else 0 end),
TramProgBook = sum(case when b.CalendarDate <= shft.CalendarDate and b.CalendarDate >= shft.BeginDate then
isnull(b.TrammedOutReefTons,0) else 0 end),
BookSimbaMetresDrilled = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.SimbaMetresDrilled,0) else 0 end),
BookSimbaMetresFC = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.SimbaMetresForecast,0) else 0 end),
LargeEndsFC = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.LargeEndsMetresForecast,0) else 0 end),
SmallEndFC = sum(case when b.CalendarDate = shft.CalendarDate then isnull(b.SmallEndsMetresForecast,0) else 0 end),
TonsLoadedLH = sum(0),
BackfillPlanDaily = sum(0),
BackfillPlanProg = sum(0),
BackfillBookDaily = sum(0),
BackfillBookProg = sum(0),
CostingActivity = max(''),
LargeEnd = max(''),
SmallEnd = max(''),
Critical = max('')
from vw_Shifts_MO shft
right join BookingDaily b on
b.SectionID = shft.SectionID_2 and
b.CalendarDate = shft.CalendarDate
left outer join vw_DailyProductionData d on
shft.ProdMonth = d.ProdMonth and
b.SectionID = d.SectionID_2
where shft.ProdMonth = @ProdMonth
group by shft.ProdMonth, shft.SectionID_2, shft.Name_2,
d.SectionID, d.Name_1,
d.WPID, d.Description, shft.CalendarDate, shft.ShiftNo
union All
--declare @ProdMonth varchar(6)
--set @ProdMonth = '201304'
select p.ProdMonth, p.SectionID, shft.Name_2,
o.OreFlowID SectionID, o.Name Name_1,
'' WPID, '' Description, shft.ShiftNo,
shft.CalendarDate,
Activity = ca.Description,
Rigg = max(''),
CostAccount = max(''),
OpsPlan_Metres = max(p.Metres),
OpsPlan_Tons = max(p.Tons),
OpsPlan_GT = max(p.GT),
OpsPlan_Kg = max(p.Kg),
PlanDynMetres = sum(0),
PlanDynGT = sum(0),
PlanDynVolume = sum(0),
PlanDynKG = sum(0),
PlanDynDesSqm = sum(0),
PlanLockMetres = sum(0),
PlanLockGT = sum(0),
PlanLockVolume = sum(0),
PlanLockKG = sum(0),
PlanLockDesSqm = sum(0),
PlanProgAdv = sum(0),
PlanDayAdv = sum(0),
PlanDayReefMetres = sum(0),
PlanDayWasteMetres = sum(0),
EndHeight = sum(0),
EndWidth = sum(0),
PlanProgVolume = sum(0),
PlanDayVolume = sum(0),
PlanProgTons = sum(0),
PlanDayTons = sum(0),
PlanDayReefTons = sum(0),
PlanDayWasteTons = sum(0),
PlanGT = sum(0),
PlanProgKG = sum(0),
PlanDayKG = sum(0),
BookDayAdv = sum(0),
BookProgAdv = sum(0),
BookDayVolume = sum(0),
BookProgVolume = sum(0),
BookDayDesSqm = sum(0),
BookDayTons = sum(0),
BookProgTons = sum(0),
BookDayKg = sum(0),
BookProgKg = sum(0),
BookGT = sum(0),
SimbaDay = sum(0),
TramDayPlan = sum(0),
TramProgPlan = sum(0),
TramDayBook = sum(0),
TramProgBook = sum(0),
BookSimbaMetresDrilled = sum(0),
BookSimbaMetresFC = sum(0),
LargeEndsFC = sum(0),
SmallEndFC = sum(0),
TonsLoadedLH = sum(0),
BackfillPlanDaily = sum(0),
BackfillPlanProg = sum(0),
BackfillBookDaily = sum(0),
BackfillBookProg = sum(0),
CostingActivity = max(''),
LargeEnd = max(''),
SmallEnd = max(''),
Critical = max('')
from OpsPlan p
inner join vw_Shifts_MO shft on
p.ProdMonth = shft.ProdMonth and
p.SectionID = shft.SectionID_2
left outer join CODE_ACTIVITY ca on
ca.Activity = p.Activity
left join OreflowEntities o on
p.ProjectID = o.OreFlowID and
o.OreFlowCode = 'Proj'
where p.ProdMonth = @ProdMonth
group by p.ProdMonth, p.SectionID, shft.Name_2, o.OreFlowID, o.Name, ca.Description,
shft.CalendarDate, shft.ShiftNo
) a
group by ProdMonth,
SectionID_2, Name_2, WPID, Description, CalendarDate, ShiftNo