我放弃了之前所有者的所有工作,并从头开始在tsql上工作。回顾一下,我试图用多个废料记录的总和以及多个停机记录的总和来加入单个状态记录。以下是迭代:
-- DOWNTIME SUMMARY, sum downtime by shift
SELECT ShiftReportDowntime.DTR_SRID, Sum(ShiftReportDowntime.DTR_DownTimeDuration) AS SumOfDTR_DownTimeDuration
FROM ShiftReportDowntime
GROUP BY ShiftReportDowntime.DTR_SRID;
-- PART COUNT SUMMARY, get part counts by shift (yes, this table was poorly constructed)
SELECT ShiftReportPartCount.SR_PC_SRID, ShiftReportPartCount.ShiftFTTQ, ShiftReportPartCount.ShiftOEE, isnull([SR_PC_IsParts1],0)+isnull([SR_PC_IsParts2],0)+isnull([SR_PC_IsParts3],0)+isnull([SR_PC_IsParts4],0)+isnull([SR_PC_IsParts5],0)+isnull([SR_PC_IsParts6],0)+isnull([SR_PC_IsParts7],0)+isnull([SR_PC_IsParts8],0)+isnull([SR_PC_IsParts9],0) as Produced
FROM ShiftReportPartCount;
-- SCRAP COUNT SUMMARY, sum scrap count by shift
SELECT Sum(ShiftReportScrap.SRS_Scraped) AS SumOfSRS_Scraped
FROM Traceabillity INNER JOIN ShiftReportScrap ON Traceabillity.OT_ID = ShiftReportScrap.SRS_PartID
GROUP BY Traceabillity.OT_PRT_ID, ShiftReportScrap.SRS_SR_ID;
------------------------------------------------------------------------------
-- START OVER, JUST THE STATUS
SELECT V.Equ_Name as Station, V.SR_Shift as Shift,
case V.[SR_ShiftStatus]
when 0 then 'restart'
when 1 then 'G- On Time'
when 2 then 'G- Late'
when 3 then 'R- On Time'
when 4 then 'R- Late'
when 5 then 'Closed'
when 6 then 'Down'
else 'No Info'
end as "Status",
V.PRT_Number as [Part Number],
V.PRT_Description as [Part Description],
convert(varchar,V.SR_StartTime,100) as [Shift Start],
V.SR_ID,
V.SR_ShiftStatus as StatusId
FROM V_ShiftReportMaster V
WHERE (((V.SR_ShiftStatus)<>5))
ORDER BY V.Equ_Name, V.SR_Shift;
-- NOW STATUS WITH DOWNTIME
SELECT V.Equ_Name as Station, V.SR_Shift as Shift,
case V.[SR_ShiftStatus]
when 0 then 'restart'
when 1 then 'G- On Time'
when 2 then 'G- Late'
when 3 then 'R- On Time'
when 4 then 'R- Late'
when 5 then 'Closed'
when 6 then 'Down'
else 'No Info'
end as "Status",
V.PRT_Number as [Part Number],
V.PRT_Description as [Part Description],
convert(varchar,V.SR_StartTime,100) as [Shift Start],
D.DTmin,
V.SR_ID,
V.SR_ShiftStatus as StatusId
FROM V_ShiftReportMaster V LEFT JOIN
(SELECT DTR_SRID, isnull(Sum(DTR_DownTimeDuration),0) AS DTmin
FROM ShiftReportDowntime
GROUP BY DTR_SRID) D ON (D.DTR_SRID=V.SR_ID)
WHERE (((V.SR_ShiftStatus)<>5))
ORDER BY V.Equ_Name, V.SR_Shift;
-- NOW STATUS WITH DOWNTIME, SCRAP COUNT
-- V= V_ShiftReportMaster, D= downtime, C=count
SELECT V.Equ_Name as Station, V.SR_Shift as Shift,
case V.[SR_ShiftStatus]
when 0 then 'restart'
when 1 then 'G- On Time'
when 2 then 'G- Late'
when 3 then 'R- On Time'
when 4 then 'R- Late'
when 5 then 'Closed'
when 6 then 'Down'
else 'No Info'
end as "Status",
V.PRT_Number as [Part Number],
V.PRT_Description as [Part Description],
convert(varchar,V.SR_StartTime,100) as [Shift Start],
D.DTmin,
c.Produced,
c.FTTQ,
c.OEE,
V.SR_ID,
V.SR_ShiftStatus as StatusId
FROM V_ShiftReportMaster V LEFT JOIN (
SELECT DTR_SRID, isnull(Sum(DTR_DownTimeDuration),0) AS DTmin
FROM ShiftReportDowntime
GROUP BY DTR_SRID) D ON (D.DTR_SRID=V.SR_ID
) LEFT JOIN (
SELECT SR_PC_SRID, isnull(ShiftReportPartCount.ShiftFTTQ,0) as FTTQ, isnull(ShiftReportPartCount.ShiftOEE,0) as OEE, isnull([SR_PC_PlanParts1],0)+isnull([SR_PC_PlanParts2],0)+isnull([SR_PC_PlanParts3],0)+isnull([SR_PC_PlanParts4],0)+isnull([SR_PC_PlanParts5],0)+isnull([SR_PC_PlanParts6],0)+isnull([SR_PC_PlanParts7],0)+isnull([SR_PC_PlanParts8],0)+isnull([SR_PC_PlanParts9],0) as Produced
FROM ShiftReportPartCount) C ON (C.SR_PC_SRID=V.SR_ID)
WHERE (((V.SR_ShiftStatus)<>5))
ORDER BY V.Equ_Name, V.SR_Shift;
-- NOW STATUS WITH DOWNTIME, SCRAP COUNT, AND PRODUCED COUNT - WORKING
-- V= V_ShiftReportMaster, D= downtime, C=count, S= scrap count
SELECT V.Equ_Name as Station, V.SR_Shift as Shift,
case V.[SR_ShiftStatus]
when 0 then 'restart'
when 1 then 'G- On Time'
when 2 then 'G- Late'
when 3 then 'R- On Time'
when 4 then 'R- Late'
when 5 then 'Closed'
when 6 then 'Down'
else 'No Info'
end as "Status",
V.PRT_Number as [Part Number],
V.PRT_Description as [Part Description],
isnull(c.[Total Produced],0) as [Total Produced],
isnull(s.Scrap,0) as Scrap,
isnull(c.[Total Produced],0)-isnull(s.Scrap,0) as [Total Good],
isnull(cast(cast((C.ShiftFTTQ*100)as decimal(18,2))as varchar(8))+'%',0) as FTTQ,
isnull(cast(cast((C.ShiftOEE*100)as decimal(18,2))as varchar(8))+'%',0) as OEE,
isnull(D.DTmin,0) as DTmin,
convert(varchar,V.SR_StartTime,100) as [Shift Start],
V.SR_ID,
V.SR_ShiftStatus as StatusId
FROM V_ShiftReportMaster V LEFT JOIN (
SELECT DTR_SRID, isnull(Sum(DTR_DownTimeDuration),0) AS DTmin
FROM ShiftReportDowntime
GROUP BY DTR_SRID) D ON (D.DTR_SRID=V.SR_ID
) LEFT JOIN (
SELECT SR_PC_SRID, ShiftFTTQ, ShiftOEE, isnull([SR_PC_IsParts1],0)+isnull([SR_PC_IsParts2],0)+isnull([SR_PC_IsParts3],0)+isnull([SR_PC_IsParts4],0)+isnull([SR_PC_IsParts5],0)+isnull([SR_PC_IsParts6],0)+isnull([SR_PC_IsParts7],0)+isnull([SR_PC_IsParts8],0)+isnull([SR_PC_IsParts9],0) as [Total Produced]
FROM ShiftReportPartCount) C ON (C.SR_PC_SRID=V.SR_ID
) LEFT JOIN (
SELECT Sum(ShiftReportScrap.SRS_Scraped) AS Scrap,Traceabillity.OT_PRT_ID, ShiftReportScrap.SRS_SR_ID
FROM Traceabillity INNER JOIN ShiftReportScrap ON Traceabillity.OT_ID = ShiftReportScrap.SRS_PartID
GROUP BY Traceabillity.OT_PRT_ID, ShiftReportScrap.SRS_SR_ID
) S ON (S.SRS_SR_ID=V.SR_ID AND S.OT_PRT_ID=V.SR_PartID)
WHERE (((V.SR_ShiftStatus)<>5))
ORDER BY V.Equ_Name, V.SR_Shift;