我如何通过相同的顺序,文章和不同的操作从总和值中减去最小值

时间:2017-06-14 11:09:04

标签: c# sql

以下是它的外观示例。我希望在存储过程中这样做,但我对如何检测新操作并减去最小值感到困惑。

  SELECT order_name, 
         article_name, 
         operation, 
         SUM(qty_pack) AS Controled, 
         SUM(repaired) AS Repaired
    FROM table_PrintJob 
   WHERE scanned = '1' AND 
         date_time BETWEEN @start_time AND 
         @end_time 
           --CONVERT(VARCHAR(10),date_time, 110)>=@start_time AND 
         CONVERT(VARCHAR(10),date_time, 110)<=@end_time 
GROUP BY order_name, 
         article_name, 
         operation

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用MIN()作为窗口功能:

SELECT order_name, article_name, operation,
       SUM(qty_pack) AS Controled, SUM(repaired) AS Repaired,
       (CASE WHEN COUNT(*) OVER (PARTITION BY order_name, article_name) > 1
             THEN SUM(qty_pack) -
                  MIN(SUM(qty_pack)) OVER OVER (PARTITION BY order_name, article_name)
             ELSE SUM(qty_pack)
        END) as massaged_value
FROM table_PrintJob 
WHERE scanned = '1' AND
      date_time BETWEEN @start_time AND @end_time 
GROUP BY order_name, article_name, operation