Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread Explicit order clause?
Mon, Feb 1 2016 3:06 PMPermanent Link

Gregory T Swain

Zucchetti North America LLC

Is there any way for an explicit sort order? Ex:

Select ID, Name, Val from TblMyTable order by Name ("john", "andrew", "bill");

|  ID  |  NAME  | VAL
--------------------------------
  3   |  John     | 'abc'
  1   |  Andrew | '321'
  2   | Bill        | NULL
Mon, Feb 1 2016 4:43 PMPermanent Link

Jeff Cook

Aspect Systems Ltd

Avatar

On 2/02/2016 9:06 a.m., Gregory T Swain wrote:
> Is there any way for an explicit sort order? Ex:
>
> Select ID, Name, Val from TblMyTable order by Name ("john", "andrew", "bill");
>
> |  ID  |  NAME  | VAL
> --------------------------------
>     3   |  John     | 'abc'
>     1   |  Andrew | '321'
>     2   | Bill        | NULL
>

How about:-

SELECT ID,
       Name,
       Val,
       IF(Name = 'john'        THEN 1
       ELSE IF(Name = 'andrew' THEN 2
       ELSE IF(Name = 'bill  ' THEN 3
       ELSE 0))) AS Seq

-- probably a CASE statement instead of the IF's

FROM TblMyTable
ORDER BY seq, ID

HTH

Jeff
Tue, Feb 2 2016 5:29 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Gregory


If you only have a few names/records to bother about I'd use Jeff's approach. If there are more I'd add in a computed column and come up with a mechanism to generate a value based on the name - like soundex but customised to your needs and then sort of the computed column.

Roy Lambert
Image