The things I learned today

Tuesday, November 08, 2005

how to combine resultsets horizontally in sp
Hi all, I have a rather newbie question on stored proc. How can I can combine totally resultsets from two different queries and return them as one, assuming both queries return just one row. (I want to do a horizontal combine, as opposed to a vertical UNION combine)

For example, I would like to combine the two below:
e.g.
select count(*) as users as from users where userid <= 1000

select count(*) as total_users from users

so the result set returns users and total_users.

----------------------------------------

You can use
SELECT (select count(*) from users where userid <= 1000 ) As Users,
(select count(*) from users) As total_users

There may be other patterns such as an outer join that will work also.


-----------------------------------------

Hi Boon,

Here's one way of doing it:

SELECT A.Users, B.Total_Users
FROM (SELECT COUNT(*) AS Users FROM Users WHERE UserID <= 1000) A,
(SELECT COUNT(*) AS Total_Users FROM Users) B

Hope this helps.

------------------------------------------


Something like this
Declare @users INT
Declare @tortalusers int
SET NOCOUNT ON
Select @users=count(1) from users where userid <= 1000
Select @totalusers=count(*) from users
Select @users,@totalusers