选择查询中的Tsql两个按语句分组

时间:2016-07-14 15:47:25

标签: sql-server tsql

我被困在尝试通过子查询添加多个组到我所做的查询(我的Tsql技能是新生的)并且可以使用一些帮助。 我试图将查询“停机时间”结果合并到查询“状态”中,以便显示的每个V_ShiftReportMaster记录也将显示ShiftReportDowntime条目(DTmin)的总和。 我一直在阅读其他问题/答案,但这对我来说还没有联系 - 我可以在我的例子中使用一些帮助。

(左)连接列是: V_ShiftReportMaster.SR_ID(一条记录)= ShiftReportDowntime.DTR_SRID(许多记录)

- 查询DOWNTIME

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

- 查询状态

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;

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分: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;