Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get rows in order predicates appear in where clause? 1

Status
Not open for further replies.

Coder7

Programmer
Oct 29, 2002
224
US
Is there a way to get the rows returned in the order in which the predicates appear in the where clause IE

THE QUERY:
select PRED from TABLEA
where PRED = 9
or PRED = 21
or PRED = 3

THE RESULT SET:
9
21
3

Thanks for any insight!
 
Coder7,

I cannot see how you can determine a different sort on the same column in one pass.
This can be done using a SProc, and the values would need to be passed as either a string of values (like an array) or as seperate params, due to the requirement to order by.
Using the SQL statement as above, load a temp table with the results you want. Because the order by may not be in numerical order, you will need to UNION result sets together to get the right order.
e.g.
select PRED from #TempTABLEA where PRED = @param1
UNION ALL
select PRED from #TempTABLEA where PRED = @param2
UNION ALL
select PRED from #TempTABLEA where PRED = @param2


(this would be where @param1=9, @param2=23, @param3=3)

Hope this assists

Logicalman
 
Hi,

Try this query....

select PRED from TABLEA
where PRED = 9
or PRED = 21
or PRED = 3
Order By Case PRED When 9 then 1
When 21 then 2
When 3 then 3
End

Hope it helps

Sunil
 
Thanks for the input LM and Sunila.

Sunila's method seems less complex - earns you a star.

 
Thanks for that info, SQL Sister.

I'm always looking for ways to improve performance.

Have a great week everyone!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top