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