troubleshooting Question

SQL Query - Max Value

Avatar of holemania
holemania asked on
SQL
2 Comments1 Solution239 ViewsLast Modified:
I need help with a query.  I have a nested query that pull from 2 tables with a union all.  I want to combine the 2 record it pull, but the greater value of the 2 into 1 record.  Or more like if it's NULL then use the other value from the other record.

See my query and example output.
SELECT 	X.ID, X.ORDER_ID, X.DATE, X.STATUS, X.ORDER_POLICY
FROM	(
	SELECT 	ITEM_ID AS ID, ORDER_ID, TRANSACTION_DATE AS DATE, NULL AS STATUS, AS NULL ORDER_POLICY
	FROM	SALES_ORDER
	WHERE	
	UNION ALL
	SELECT	ID, NULL AS ORDER_ID, NULL AS DATE, NULL AS STATUS, ORDER_POLICY
	FROM	ITEMS
	) X
WHERE	X.ID = '129892' AND ISNULL(NULLIF(X.ORDER_ID, ''), '' = 'SO12342'
output-sample.jpg
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros