2016-07-14 12 views
0

私は作成したクエリに複数のサブクエリを追加しようとしています(私のTsqlスキルは新生児です)。 表示された各V_ShiftReportMasterレコードにShiftReportDowntimeエントリ(DTmin)の合計も表示されるように、クエリ「Status」にクエリ「Downtime」の結果を組み込もうとしています。 私は他の質問/回答を通じて読んできましたが、これはまだ私のために接続していません - 私は実際に私の例でいくつかの助けを使用することができます。選択クエリの2つのグループ文によるTsql 2つのグループ化文

(左)結合列は以下のとおりです。 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 STATUS

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で最初から開始しました。要約すると、複数のスクラップレコードの合計と複数のダウンタイムレコードの合計で1つのステータスレコードに参加しようとしています。以下は反復です:

-- 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; 
関連する問題