Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 10 total
Thread ARBITRARY ORDER BY
Tue, May 26 2009 8:58 PMPermanent Link

oliver
Hello All,

Seems this forum is full of knowledgable people. Here's what I'd like to know how to do:

I would like to write a SELECT statement that contains an ORDER BY clause on a particular column
where the ordering would be according to a predetermined order that is not necessarily alphabetical or numeric or
a combination of both.

So for example if the column in question for the ORDER BY clause contains several records with
the string 'CAR' and others with the string 'DOG' and others with the string 'MOUSE' I would like to be
able to explicitely states that the DOG records should be ordered first, then CAR records, then MOUSE records,
then whatever is left over that is not any of the above. On a different SELECT of the same data I'd perhaps like MOUSE to come first, etc.

Is there an easy way to do this ?

Thanks

Oliver
Tue, May 26 2009 10:11 PMPermanent Link

"Jeff Cook"
oliver wrote:

> Hello All,
>
> Seems this forum is full of knowledgable people. Here's what I'd like
> to know how to do:
>
> I would like to write a SELECT statement that contains an ORDER BY
> clause on a particular column where the ordering would be according
> to a predetermined order that is not necessarily alphabetical or
> numeric or a combination of both.
>
> So for example if the column in question for the ORDER BY clause
> contains several records with the string 'CAR' and others with the
> string 'DOG' and others with the string 'MOUSE' I would like to be
> able to explicitely states that the DOG records should be ordered
> first, then CAR records, then MOUSE records, then whatever is left
> over that is not any of the above. On a different SELECT of the same
> data I'd perhaps like MOUSE to come first, etc.
>
> Is there an easy way to do this ?
>
> Thanks
>
> Oliver

Kia Orana Oliver

This is an example using v3.30 syntax ... probably with errors Wink

SELECT IF(Field1 = 'DOG' THEN '01'
      ELSE IF(Field1 = 'CAT' THEN '02'
     ELSE IF(Field1 = 'MOUSE' THEN '03'
      ELSE '99'))) AS Sequence.
     OtherField
FROM MyTable
ORDER BY Sequence, ...

v4 probably does this prettily with a CASE statement or whatever.

Cheers

Jeff


--
Jeff Cook
Aspect Systems Ltd
www.aspect.co.nz
+
Joan and Jeff Cook
The Cooks Oasis
www.cookislandsoasis.com
Wed, May 27 2009 2:20 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Jeff

If the strings are unique and guaranteed how about

SELECT POS(Field1,'DOG;CAT;'MOUSE) AS Sequence, etc FROM MyTable ORDER BY Sequence,

As a bonus should give the unknowns at the top

Roy Lambert [Team Elevate]
Wed, May 27 2009 7:58 AMPermanent Link

"John Hay"

Roy

> SELECT POS(Field1,'DOG;CAT;'MOUSE) AS Sequence, etc FROM MyTable ORDER BY
Sequence,

Nearly works for options as given but not so good with MEERCAT or DORMOUSE
added in front <bg>

John

Wed, May 27 2009 8:30 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


easy


SELECT POS(':' + Field1 + ':' , ';DOG;MEERCAT;CAT;'MOUSEWinkAS Sequence, etc FROM MyTable ORDER BY Sequence


Roy Lambert
Wed, May 27 2009 9:35 AMPermanent Link

"John Hay"
Roy
> easy
>
>
> SELECT POS(':' + Field1 + ':' , ';DOG;MEERCAT;CAT;'MOUSEWinkAS Sequence,
etc FROM MyTable ORDER BY Sequence

Yes - if you want an 11949 or all not found <vbg>

John

Wed, May 27 2009 10:34 AMPermanent Link

Fernando Dias

Team Elevate Team Elevate

Oliver,

SELECT
  YourColumn1,
  ...
  YourColumnN,
  CASE
    WHEN POSITION('DOG', UPPER(TheColumn)) > 0 THEN 1
    WHEN POSITION('CAR', UPPER(TheColumn)) > 0 THEN 2
    WHEN POSITION('MOUSE', UPPER(TheColumn)) > 0 THEN 3
  ELSE
    0
  END AS OrderNumber
FROM
  YourTable
ORDER BY
  OrderNumber


--
Fernando Dias
[Team Elevate]
Wed, May 27 2009 10:35 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


Good grief some people need their hands holding Smiley

select POS('*'+_surname+'*','*Smith*Jones*Watling*') as Seq,_surname from contacts order by seq

The problem with the other one was that I used a semicolon as a separator and the parser has a small fit <vbg>

Roy Lambert [Team Elevate]
Wed, May 27 2009 1:00 PMPermanent Link

"John Hay"

Roy

> Good grief some people need their hands holding Smiley

LOL

I didn't even notice the semi colons.  It was the misplaced quote to which I
was referring.

John

Thu, May 28 2009 3:41 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


Fair cop guv - I never even noticed the misplaced quote Smiley

Roy Lambert
Image