[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to recreate a union sql query to be one row

Posted on 2008-11-17
6
Medium Priority
?
606 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:HarleySkater
  • 3
  • 2
6 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22978549
well, if it is the same data returned, you could use UNION ALL instead of UNION.
0
 
LVL 1

Author Comment

by:HarleySkater
ID: 22978575
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
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 22978636
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 1

Author Comment

by:HarleySkater
ID: 22978644
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
 
LVL 1

Author Closing Comment

by:HarleySkater
ID: 31517563
2 solutions.   haha making us all look bad :D

THANKS!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22978728
>>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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question