【问题标题】:Tsql Two Group By Statements in Select Query选择查询中的Tsql两个Group By语句
【发布时间】:2016-07-21 20:45:52
【问题描述】:

我被困在尝试将多个按子查询添加到我所做的查询中(我的 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;

【问题讨论】:

    标签: sql-server tsql


    【解决方案1】:

    我建议你不要害怕使用一些空白。它使您的查询更容易阅读。此外,使用别名将使事情更容易使用。这是我将如何格式化该查询以使其更加清晰(我必须对其进行格式化,以便我什至可以开始理解它)。

    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/

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        相关资源
        最近更新 更多