how to recreate a union sql query to be one row

I have to do a select from 2 different users and return the data on the same row, not 2 separate rows

I'm trying but I keep getting unwanted returns
Create PROCEDURE [dbo].[Notification]
 
	@SenderId uniqueidentifier,
	@RecipientId uniqueidentifier
 
 
AS
 
	SELECT  aspnet_Membership.LoweredEmail as RecipientEmail
	FROM         aspnet_Membership
	WHERE aspnet_Membership.UserId = @RecipientId
UNION
	SELECT  ag_profiles.RealName as SenderName	
	FROM         ag_profiles
	WHERE ag_profiles.UserId = @SenderId
 
		
RETURN

Open in new window

LVL 1
HarleySkaterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
well, if it is the same data returned, you could use UNION ALL instead of UNION.
0
HarleySkaterAuthor Commented:
2 different users information from 2 different tables, but I need it to return 1 set of results.  I tried union all and it returned 2 rows also :X

Thanks though :D
0
Kevin CrossChief Technology OfficerCommented:
You can try something like this:


SELECT  aspnet_Membership.LoweredEmail as RecipientEmail, sender.SenderName
FROM         aspnet_Membership
OUTER APPLY (
	SELECT  ag_profiles.RealName as SenderName	
	FROM         ag_profiles
	WHERE ag_profiles.UserId = @SenderId
) sender
WHERE aspnet_Membership.UserId = @RecipientId
 
-- OR in normal subquery --
 
SELECT  aspnet_Membership.LoweredEmail as RecipientEmail
, (
	SELECT TOP 1 ag_profiles.RealName
	FROM         ag_profiles
	WHERE ag_profiles.UserId = @SenderId
) As SenderName
FROM         aspnet_Membership
WHERE aspnet_Membership.UserId = @RecipientId

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

HarleySkaterAuthor Commented:
sorry not SET - I meant row -  
2 different users information from 2 different tables, but I need it to return 1 row!!!!!!!!!!!!!!!!!!!.  I tried union all and it returned 2 rows also :X

Thanks though :D
0
HarleySkaterAuthor Commented:
2 solutions.   haha making us all look bad :D

THANKS!
0
Kevin CrossChief Technology OfficerCommented:
>>Grading Comments: 2 solutions. haha making us all look bad :D THANKS!

LOL.

Firstly, you are welcome.  Just glad to help.  

Secondly, showed the outer apply as it will allow you to use multiple columns from second table.  It applies that data to every record in the outer query which probably fits in your case since you usually have one sender and one-many recipients.  The subquery approach only gets you one column.

;) Kev
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.