SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Sorting a recordset

    Hi

    I have the following query

    Code:
    SELECT [eventid], [name],[date] FROM events WHERE eventid IN(22,13,18)
    I'd like to ensure the records are returned sorted by the eventid in the order the eventid's appear in the IN statement.

    EG:

    22 | Event ABC | 28/09/2012
    13 | Event XYZ | 12/08/2012
    18 | Event CDE | 22/09/2012

    Is this possible?
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    very possible
    Code:
    ORDER
        BY CASE WHEN eventid = 22 THEN 'curly
                WHEN eventid = 13 THEN 'larry'
                                  ELSE 'moe' 
            END
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    very possible
    Code:
    ORDER
        BY CASE WHEN eventid = 22 THEN 'curly
                WHEN eventid = 13 THEN 'larry'
                                  ELSE 'moe' 
            END
    Thanks Rudy, and I should have probably made this clear in my original post, is that valid for Access SQL or is some bizarre syntax required?
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  4. #4
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Scratch that I think I have it!
    Code:
    SELECT eventid,[name],[date] FROM events WHERE eventid IN(22,18,13) ORDER BY IIF(eventid = 22,'curly',IIF(eventid = 13, 'larry','moe'))
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    ace! (you got it in one)

    p.s. if you add a 4th event id, you can use 'shemp' -- but after that, curly joe and joe would be out of sequence
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    ace! (you got it in one)

    p.s. if you add a 4th event id, you can use 'shemp' -- but after that, curly joe and joe would be out of sequence
    Thanks, fortunately I only need to go to 3.
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •