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?
 
Kevin CrossConnect With a Mentor Chief 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
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.