sql server
-
Monday, February 18, 2013 3:04 PM
Hi All,
I am executing 2sql statement in that 2 sql statements i am combining union operator
so i am getting 2outputs but i want 1 output so i am puting subquery as top1
instead of using subquery how to output as single row query
Thanks
Ram
- Changed Type David DyeModerator Monday, February 18, 2013 3:19 PM Post requests assistance and ask a question as to the UNION set operator
- Moved by David DyeModerator Monday, February 18, 2013 3:19 PM Post is more specific to T-SQL as it requests assistance on using UNION
All Replies
-
Monday, February 18, 2013 3:18 PMModerator
If you are using a UNION set operator to join two queries then you should be receiving a single result set and not two. Please post the SQL statement to provide better assistance.
David Dye My Blog
-
Monday, February 18, 2013 3:19 PM
What is your query?
Also, this question is better put in the T-SQL forum. http://social.msdn.microsoft.com/Forums/en-US/transactsql/threads
Russel Loski, MCT, MCSA SQL Server 2012, 2008, MCITP Business Intelligence Developer and Database Developer 2008 Twitter: @sqlmovers; blog: www.sqlmovers.com
-
Monday, February 18, 2013 3:29 PMModerator
Hi Ram,
UNION operator does not guarantee that 2 rows will be in the result set. But UNION ALL does.
To get only one result, selecting TOP 1 or using Row_Number() function in CTE with condition set to 1 is a good choice
Here is a sample
WITH cte AS (SELECT 30 AS id UNION SELECT 20 AS id) SELECT TOP (1) id FROM cte AS cte_1 ORDER BY idbetter than
with cte as ( SELECT 30 AS id UNION SELECT 20 AS id ), cte2 as ( select *, rn=Row_Number() over (order by id) from cte ) select * from cte2 where rn=1
- Marked As Answer by Iric WenModerator Tuesday, February 26, 2013 8:38 AM
-
Monday, February 18, 2013 6:29 PM
Share your query for understading exact issue.
If you are using UNION operator at right place then you must get only one consolidate output not two.
Regards,
Rohit Garg
(My Blog)
This posting is provided with no warranties and confers no rights.
Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread.

