Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » DBISAM Technical Support » Support Forums » DBISAM SQL » View Thread |
Messages 1 to 10 of 10 total |
ARBITRARY ORDER BY |
Tue, May 26 2009 8:58 PM | Permanent 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 PM | Permanent 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 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 AM | Permanent Link |
Roy Lambert NLH Associates 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 AM | Permanent 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 AM | Permanent Link |
Roy Lambert NLH Associates Team Elevate | John
easy SELECT POS(':' + Field1 + ':' , ';DOG;MEERCAT;CAT;'MOUSEAS Sequence, etc FROM MyTable ORDER BY Sequence Roy Lambert |
Wed, May 27 2009 9:35 AM | Permanent Link |
"John Hay" | Roy
> easy > > > SELECT POS(':' + Field1 + ':' , ';DOG;MEERCAT;CAT;'MOUSEAS 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 AM | Permanent Link |
Fernando Dias 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 AM | Permanent Link |
Roy Lambert NLH Associates Team Elevate | John
Good grief some people need their hands holding 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 PM | Permanent Link |
"John Hay" | Roy > Good grief some people need their hands holding 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 AM | Permanent Link |
Roy Lambert NLH Associates Team Elevate | John
Fair cop guv - I never even noticed the misplaced quote Roy Lambert |
This web page was last updated on Tuesday, April 30, 2024 at 03:55 PM | Privacy PolicySite Map © 2024 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |