2016-07-14 71 views
0

我被困在试图通过子查询添加多个组到我做的查询(我的Tsql技能是新生的),并可以使用一些帮助。 我试图将查询“停机时间”结果合并到查询“状态”中,以便每个显示的V_ShiftReportMaster记录还将显示ShiftReportDowntime条目(DTmin)的总和。 我一直在阅读其他问题/答案,这只是不连接我 - 我真的可以用我的例子一些帮助。Tsql在选择查询中的两个组按语句

的(左)参加列有: V_ShiftReportMaster.SR_ID(一个记录)= ShiftReportDowntime.DTR_SRID(多条记录)

--query停机

SELECT ShiftReportDowntime.DTR_SRID, Sum(ShiftReportDowntime.DTR_DownTimeDuration) AS DTmin 
FROM ShiftReportDowntime 
GROUP BY ShiftReportDowntime.DTR_SRID; 

--query状态

SELECT V_ShiftReportMaster.Equ_Name as Station, V_ShiftReportMaster.SR_Shift as Shift, 
case V_ShiftReportMaster.[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_ShiftReportMaster.PRT_Number as [Part Number], V_ShiftReportMaster.PRT_Description as [Part Description], (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)) AS [Produced Count], ISNULL(a.SumOfSRS_Scraped,0) AS [Scrap Count], (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(a.SumOfSRS_Scraped,0) AS [Good Count], isnull(cast(cast((ShiftReportPartCount.ShiftFTTQ*100)as decimal(18,2))as varchar(8))+'%',0) as FTTQ, isnull(cast(cast((ShiftReportPartCount.ShiftOEE*100)as decimal(18,2))as varchar(8))+'%',0) as OEE, convert(varchar,V_ShiftReportMaster.SR_StartTime,100) as [Shift Start], V_ShiftReportMaster.SR_ID, V_ShiftReportMaster.SR_ShiftStatus as StatusId 
FROM (V_ShiftReportMaster LEFT JOIN ShiftReportPartCount ON V_ShiftReportMaster.SR_ID = ShiftReportPartCount.SR_PC_SRID) LEFT JOIN (SELECT ShiftReportScrap.SRS_SR_ID, Sum(ShiftReportScrap.SRS_Scraped) AS SumOfSRS_Scraped, Traceabillity.OT_Number 
FROM Traceabillity RIGHT JOIN ShiftReportScrap ON Traceabillity.OT_ID = ShiftReportScrap.SRS_PartID 
GROUP BY ShiftReportScrap.SRS_SR_ID, Traceabillity.OT_Number) A ON (A.SRS_SR_ID =V_ShiftReportMaster.SR_ID and a.OT_Number = V_ShiftReportMaster.PRT_Number) 
WHERE (((V_ShiftReportMaster.SR_ShiftStatus)<>5)) 
ORDER BY V_ShiftReportMaster.Equ_Name, V_ShiftReportMaster.SR_Shift; 

回答

0

我建议你不要害怕使用一些空白区域。它使您的查询更容易阅读。而且,使用别名会使事情变得更容易。这里是我如何格式化该查询,使其更清晰(我必须格式化,以便我甚至可以开始理解它)。

SELECT vrm.Equ_Name as Station 
    , vrm.SR_Shift as Shift 
    , case vrm.[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 
    , vrm.PRT_Number as [Part Number] 
    , vrm.PRT_Description as [Part Description] 
    , 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) AS [Produced Count] 
    , ISNULL(a.SumOfSRS_Scraped, 0) AS [Scrap Count] 
    , 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(a.SumOfSRS_Scraped, 0) AS [Good Count] 
    , isnull(cast(cast((srpc.ShiftFTTQ * 100)as decimal(18, 2))as varchar(8)) + '%', 0) as FTTQ 
    , isnull(cast(cast((srpc.ShiftOEE * 100)as decimal(18, 2))as varchar(8)) + '%', 0) as OEE 
    , convert(varchar, vrm.SR_StartTime, 100) as [Shift Start] 
    , vrm.SR_ID 
    , vrm.SR_ShiftStatus as StatusId 
FROM V_ShiftReportMaster srm 
LEFT JOIN ShiftReportPartCount srpc ON vrm.SR_ID = srpc.SR_PC_SRID 
LEFT JOIN 
(
    SELECT srs.SRS_SR_ID 
     , Sum(srs.SRS_Scraped) AS SumOfSRS_Scraped 
     , t.OT_Number 
    FROM Traceabillity t 
    RIGHT JOIN ShiftReportScrap srs ON t.OT_ID = srs.SRS_PartID 
    GROUP BY srs.SRS_SR_ID, t.OT_Number 
) A ON A.SRS_SR_ID = vrm.SR_ID 
    and a.OT_Number = vrm.PRT_Number 
WHERE vrm.SR_ShiftStatus <> 5 
ORDER BY vrm.Equ_Name 
    , vrm.SR_Shift; 

我还建议你不要在你的列别名中使用空格。这只是使它很难与之合作。我在这里看到的最大问题是你有一些正常化问题。你有重复违反第一范式的列。考虑一下如果你需要添加SR_PC_IsParts9会有多痛苦。您将不得不更改表和引用它的每个查询。如果这是一个单独的表格,您可以根据需要添加尽可能多的这些表格,而且您的查询将更易于编写。

至于手头的问题......我不知道你在做什么。也许这将是一个开始的好地方。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/

0

我放弃了之前所有者的所有工作,并从头开始在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;