在SQL中,我需要根据另一列汇总列的不同值

时间:2015-10-13 05:01:03

标签: sql-server sql-server-2008 sql-server-2014-express

我需要查询来给出不同ResourceId的摘要,你可以看到" G1"和一系列" M0"资源。
我想要这个" M0"资源为"新闻店"总结其预定数量为和,G1为"研磨"。
这是我的问题:

Select
    '' as Detail,
    ShiftDate,
    Max(ShiftId) as ShiftId,
    ResourceId,
    ResourceDesc,
    LineId,
    CellNo,
    Item,
    ItemDesc,
    Sum(ScheduledQty) as ScheduledQty
from (
    Select Case When b.Remarks= 'B' and cast(a.PlannedStartTime as Time) <='08:30:00.0000000' 
        Then Case When datename(dw,cast(a.PlannedStartTime as Date))='Monday'
            Then cast(a.PlannedStartTime-2 as Date)
            else cast(a.PlannedStartTime-1 as Date) end
        Else cast(a.PlannedStartTime as Date) 
    End as ShiftDate,
    b.Remarks as ShiftId,
    a.ResourceId,
    c.ResourceDesc,
    c.LineId,
    c.CellNo,
    a.RoutingId as Item,
    d.ItemDesc,
    convert(nvarchar(8),a.PlannedStartTime,114) as StartTime,
    convert(nvarchar(8),a.PlannedEndTime,114) as EndTime,
    Sum(Round(a.QtyAllocated,0)) as ScheduledQty 
From WorkOrderOpResPlan a
Left Join LocationCalendar b On a.PlantId=b.PlantId
    and Cast(a.PlannedStartTime as Time)>=Cast(b.ShiftStartTime as Time)
    and Cast(a.PlannedEndTime as Time)<=cast(b.ShiftEndTime as Time)
Inner Join ResourceMaster c on a.ResourceId=c.ResourceId
    and a.PlantId=c.PlantId and c.ResourceType='Simple'
Left Join ItemMaster d On a.RoutingId=d.Item
    and a.PlantId=d.PlantId
Where a.PlantId='SDL'
Group by
    b.Remarks,
    a.ResourceId,
    c.ResourceDesc,
    c.LineId,
    c.CellNo,
    a.RoutingId,
    d.ItemDesc,
    a.PlannedStartTime,
    a.PlannedEndTime)x
Where ShiftDate<=Cast(GetDate()+2 as Date)
Group by ShiftDate, ResourceId, ResourceDesc, LineId, CellNo, Item, ItemDesc

我想根据不同的ResourceId来汇总scheduledQty。请帮助我

这是图片enter image description here 1

1 个答案:

答案 0 :(得分:1)

请试试这个。请注意,要减少行数,您必须确定不需要哪些列值(如项目和项目描述)。

SELECT
      '' AS Detail
    , ShiftDate
    , MAX(ShiftId) AS ShiftId
    , ResourceId
    , ResourceDesc
    , LineId
    , CellNo
    , SUM(ScheduledQty) AS ScheduledQty
FROM (
      SELECT
            CASE
                  WHEN b.Remarks = 'B' AND
                  CAST(a.PlannedStartTime AS time) <= '08:30:00.0000000' THEN CASE
                        WHEN DATENAME(dw, CAST(a.PlannedStartTime AS date)) = 'Monday' THEN CAST(a.PlannedStartTime - 2 AS date)
                        ELSE CAST(a.PlannedStartTime - 1 AS date)
                  END
                  ELSE CAST(a.PlannedStartTime AS date)
            END AS ShiftDate
          , b.Remarks AS ShiftId
          , a.ResourceId
          , CASE
                  WHEN a.ResourceId = 'M0' THEN 'Press shop'
                  WHEN a.ResourceId = 'G1' THEN 'Grinding'
                  ELSE c.ResourceDesc
            END AS ResourceDesc
          , c.LineId
          , c.CellNo
          , a.RoutingId AS Item
          , SUM(ROUND(a.QtyAllocated, 0)) AS ScheduledQty
      FROM WorkOrderOpResPlan a
            LEFT JOIN LocationCalendar b ON a.PlantId = b.PlantId
                        AND CAST(a.PlannedStartTime AS time) >= CAST(b.ShiftStartTime AS time)
                        AND CAST(a.PlannedEndTime AS time) <= CAST(b.ShiftEndTime AS time)
            INNER JOIN ResourceMaster c ON a.ResourceId = c.ResourceId
                        AND a.PlantId = c.PlantId
                        AND c.ResourceType = 'Simple'
            LEFT JOIN ItemMaster d ON a.RoutingId = d.Item
                        AND a.PlantId = d.PlantId
      WHERE a.PlantId = 'SDL'
      GROUP BY
            b.Remarks
          , a.ResourceId
          , CASE
                  WHEN a.ResourceId = 'M0' THEN 'Press shop'
                  WHEN a.ResourceId = 'G1' THEN 'Grinding'
                  ELSE c.ResourceDesc
            END
          , c.LineId
          , c.CellNo
          , a.RoutingId
) x
WHERE ShiftDate <= CAST(GETDATE() + 2 AS date)
GROUP BY
      ShiftDate
    , ResourceId
    , ResourceDesc
    , LineId
    , CellNo