Results 1 to 5 of 5

Thread: MSAccess97: SELECT in ORDER BY?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    MSAccess97: SELECT in ORDER BY?

    I've got the following query:

    Code:
    SELECT a.MyID
    ,(SELECT MAX(b.MyDate) FROM MyTable2 b WHERE b.MyID=a.MyID) AS MyDateField 
    FROM MyTable1 f 
    ORDER BY (SELECT MAX(b.MyDate) FROM MyTable2 b WHERE b.MyID=a.MyID) DESC
            ,a.MyID DESC
    Which works perfectly under MSSQL, but I get a problem with the ORDER BY part when I fire it at a MSAccess..
    Doesn't MSAccess (or JET-engine) support a subquery based field in the order by, or am I doing something wrong?

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: MSAccess97: SELECT in ORDER BY?

    I don't think this is optimal anyway, as it runs the same query two times. Why don't you just order by columns number, this way :
    Code:
    SELECT a.MyID
    ,(SELECT MAX(b.MyDate) FROM MyTable2 b WHERE b.MyID=a.MyID) AS MyDateField 
    FROM MyTable1 f 
    ORDER BY 2 DESC, 1 DESC
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    Re: MSAccess97: SELECT in ORDER BY?

    Quote Originally Posted by stlaural View Post
    I don't think this is optimal anyway, as it runs the same query two times. Why don't you just order by columns number, this way :
    Code:
    SELECT a.MyID
    ,(SELECT MAX(b.MyDate) FROM MyTable2 b WHERE b.MyID=a.MyID) AS MyDateField 
    FROM MyTable1 f 
    ORDER BY 2 DESC, 1 DESC
    haha, didn't even know you could use a fieldnumber in the ORDER BY...

    when it comes to optimal I can't think of a reason why the engine would perform the subquery 2 times as it is an ORDER BY and the 'field' is in the SELECT part (but that's just my logic, which seems many times much different as that of Microsoft engineers LOL).

    thanx.

  4. #4
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: MSAccess97: SELECT in ORDER BY?

    Quote Originally Posted by SuperDre View Post
    haha, didn't even know you could use a fieldnumber in the ORDER BY...

    when it comes to optimal I can't think of a reason why the engine would perform the subquery 2 times as it is an ORDER BY and the 'field' is in the SELECT part (but that's just my logic, which seems many times much different as that of Microsoft engineers LOL).

    thanx.
    Yeah I'm not 100% sure for the optimal thing though maybe access is good enough to know that the query as already run and then not run it again. I also just noticed that you gave an alias to your columns so you could also use that to order :
    Code:
    SELECT a.MyID
    ,(SELECT MAX(b.MyDate) FROM MyTable2 b WHERE b.MyID=a.MyID) AS MyDateField 
    FROM MyTable1 f 
    ORDER BY MyDateField DESC, a.MyID DESC
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    Re: MSAccess97: SELECT in ORDER BY?

    Quote Originally Posted by stlaural View Post
    Yeah I'm not 100% sure for the optimal thing though maybe access is good enough to know that the query as already run and then not run it again. I also just noticed that you gave an alias to your columns so you could also use that to order :
    Code:
    SELECT a.MyID
    ,(SELECT MAX(b.MyDate) FROM MyTable2 b WHERE b.MyID=a.MyID) AS MyDateField 
    FROM MyTable1 f 
    ORDER BY MyDateField DESC, a.MyID DESC
    Aliases don't work in the ORDER BY or WHERE part (MS-SQL/MS-Access), I already tried that (which is also one of the Illogic items I was refering too hehe). But I think it really requeries the subquery if used in the WHERE clause..

Posting Permissions

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



Click Here to Expand Forum to Full Width