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 NumberZ, Mary, 05, 1234567 , Matt, 02, 2233445A, Bob, 10, 7654321C, Tammy, 11, 9988776and 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 @testselect 'A', 'Bob', '10', '7654321' UNION ALLselect 'Z', 'Matt', '02', '2233445' UNION ALLselect 'C', 'Tammy', '11', '9988776' UNION ALLselect '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.contactnumberfrom @test torder 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 LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-23 : 05:54:38
|
quote: Originally posted by Pesowrites "Do it in front-end application". Peter LarssonHelsingborg, Sweden
Arrgh ! too late !  KH |
 |
|
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? MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|