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)
 Syntax to group data in a certain order

Author  Topic 

froggy
Starting Member

14 Posts

Posted - 2006-10-23 : 05:37:24
Hi,

i would like to return information in the below format but not very sure how it is to be done.

Role, Name, ID, Contact Number
Z, Mary, 05, 1234567
, Matt, 02, 2233445
A, Bob, 10, 7654321
C, Tammy, 11, 9988776

and the order of Role (Z,A,C) is fixed it has to be in this order and if Role Z has more than one person, it will return only the name, id and contact number but will not repeat the role.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-23 : 05:48:10
You can easily change order by id to order by name, and in the sub query. The drawback with using names, is the likelyness of duplicates.
I hope ID is unique in your example.
declare @test table (Role varchar, Name varchar(10), ID varchar(2), ContactNumber varchar(20))

insert @test
select 'A', 'Bob', '10', '7654321' UNION ALL
select 'Z', 'Matt', '02', '2233445' UNION ALL
select 'C', 'Tammy', '11', '9988776' UNION ALL
select 'Z', 'Mary', '05', '1234567'

select case when (select count(*) from @test x where x.role = t.role and x.id < t.id) > 0 THEN NULL ELSE t.Role END Role,
t.name,
t.id,
t.contactnumber
from @test t
order by case
when t.role = 'a' then 2
when t.role = 'c' then 3
when t.role = 'z' then 1
else 32767
end,
t.id
Just wanted to post this before someone writes "Do it in front-end application".


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-23 : 05:54:38
quote:
Originally posted by Peso
writes "Do it in front-end application".

Peter Larsson
Helsingborg, Sweden



Arrgh ! too late !


KH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-23 : 06:06:17
>>Just wanted to post this before someone writes "Do it in front-end application".

Who is that Someone?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -