Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to execute multiple SELECT statements and return the output into a single cursor?

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-06-25 : 08:23:44
Bret Bonnell writes "Hi,

I am trying to return a set of records grouped by the usergroupid field. What I want to do is to return one recordset with the the 4's at the beginning of the recordset, the 9's next, and the 7's last. The query below will return one recordset, but it doesn't group them correctly. It simply orders the entire recordset by the usergroupid field. Any help would be greatly appreciated.

Thanks!


SELECT id, firstname, lastname, usergroupid FROM users
WHERE usergroupid = 4

UNION

SELECT id, firstname, lastname, usergroupid FROM users
WHERE usergroupid = 9

UNION

SELECT id, firstname, lastname, usergroupid FROM users
WHERE usergroupid = 7
ORDER BY usergroupid"

nr
SQLTeam MVY

12543 Posts

Posted - 2002-06-25 : 08:32:25
order by
case usergroupid
when 4 then 1
when 9 then 2
when 7 then 3
end

==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mzanaty
Starting Member

8 Posts

Posted - 2002-06-27 : 21:37:00
I often use a generic subgrouping template:

select 1 as ordr, ...
union
select 2 as ordr, ...
...
order by ordr
Go to Top of Page
   

- Advertisement -