Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding three statements together, need help

Status
Not open for further replies.

peels

IS-IT--Management
Joined
Aug 1, 2007
Messages
1
Location
US
I am new at this and struggling, I have these three statements that work independently, but I need to put them all together into one so that I can get the output that I desire. I appreciate any help that anyone can give me.

SELECT uvb.item_number,
COUNT(uvb.item_number) as cases_scanned
FROM t_uv_barcode uvb
WHERE uvb.barcode_uom = 'CASE' AND uvb.barcode LIKE '%C'
GROUP BY uvb.item_number


SELECT uvb.item_number,
COUNT(uvb.barcode) as cases_sold
FROM t_uv_barcode uvb
WHERE uvb.barcode_uom = 'CASE'
GROUP BY uvb.item_number


SELECT uv.item_number,
SUM(uv.qty_needed) as units_sold, SUM(uv.qty_scanned) as units_scanned
FROM t_order o (NOLOCK)
INNER JOIN t_uv_barcode_listener uv (NOLOCK)
ON o.order_number = uv.order_number
GROUP BY uv.item_number
ORDER BY uv.item_number
 
Use UNION or UNION ALL to combine the results.
 
Since you did not supply you desired output, I'm not sure what you want, but here is one option using UNION ALL as JBenson suggested:
Code:
SELECT
	item_number,
	SUM(cases_scanned),
	SUM(cases_sold),
	SUM(units_sold),
	SUM(units_scanned)
FROM
	(
		SELECT 
			uvb.item_number,
			COUNT(uvb.item_number) as cases_scanned,
			0 as cases_sold,
			0 as units_sold,
			0 as units_scanned
		FROM t_uv_barcode uvb
		WHERE uvb.barcode_uom = 'CASE' AND uvb.barcode LIKE '%C'
		GROUP BY uvb.item_number

		UNION ALL

		SELECT 
			uvb.item_number,
			0,
			COUNT(uvb.barcode) as cases_sold,
			0,
			0
		FROM t_uv_barcode uvb
		WHERE uvb.barcode_uom = 'CASE'
		GROUP BY uvb.item_number

		UNION ALL

		SELECT 
			uv.item_number,
			0,
			0,
			SUM(uv.qty_needed) as units_sold, 
			SUM(uv.qty_scanned) as units_scanned
		FROM t_order o (NOLOCK)
		INNER JOIN t_uv_barcode_listener uv (NOLOCK)
		ON o.order_number = uv.order_number
		GROUP BY  uv.item_number
	)
GROUP BY
	item_number
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top