You may simply add the two counts in an expression.
Code:
SELECT widget_name, SUM(qty_delivered) + SUM(qty_backordered)
FROM WidgetOrders
GROUP BY widget_name
This would give for each widget the number delivered plus the number on backorder. Presumably that would be the number ordered.
Possibly you wish to count the number of orders for a widget.
Code:
SELECT widget_name, SUM(qty_delivered) + SUM(qty_backordered), COUNT(*)
FROM WidgetOrders
GROUP BY widget_name
Possibly you wish to count the number of orders with deliveries and the the number of orders with backorders.
Assuming the value of qty_backordered is zero if nothing is backordered and the value of qty_delivered is zero if nothing is delivered.
Code:
SELECT widget_name,
SUM(qty_delivered) + SUM(qty_backordered),
COUNT(*),
SUM(CASE
WHEN qty_delivered = 0 THEN 0
ELSE 1
END),
SUM(CASE
WHEN qty_backordered = 0 THEN 0
ELSE 1
END)
FROM WidgetOrders
GROUP BY widget_name
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.