Results 1 to 8 of 8
  1. #1
    Cottonshirt is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2018
    Posts
    116

    How to Sort on a calculated field in a Select Query

    noob alert.



    I have an athletics database and I am trying to calculate the age (in decimal years, ex 23.8665) that an athlete won their first medal in international championship.

    I have a query that calculates this for me, with the answer in the field [diff(yd)].

    when I run the query, if I right-click the [diff(yd)] column I can select <sort Smallest to Largest> from the drop-down list and it does sort correctly. however, if I want to add sort to the query Access prompts me for a parameter [delta], which is itself a calculated field.

    the SQL looks like this:
    SELECT [tbl_400m.dat] AS dat, [tbl_athlete.fname]+" "+[tbl_athlete.sname]+" ("+[tbl_athlete.natcode]+")" AS name, tbl_athlete.birthdate, DateSerial(Year([tbl_400m.dat]),Month([tbl_athlete.birthdate]),Day([tbl_athlete.birthdate])) AS [ran(b)],
    IIf([tbl_400m.dat]-[ran(b)]>0,Year([tbl_400m.dat])-Year([tbl_athlete.birthdate]),Year([tbl_400m.dat])-Year([tbl_athlete.birthdate])-1) AS delta, IIf([tbl_400m.dat]-[ran(b)]>0,[tbl_400m.dat]-[ran(b)],365+[tbl_400m.dat]-[ran(b)]) AS days, ([delta]+([days]/365)) AS [diff(yd)]
    FROM tbl_400m INNER JOIN tbl_athlete ON (tbl_400m.sname = tbl_athlete.sname) AND (tbl_400m.fname = tbl_athlete.fname)
    WHERE (((tbl_athlete.birthdate) Is Not Null))
    ORDER BY ([delta]+([days]/365));


    I tried changing the ORDER BY command to read ORDER BY [diff(yd)]; but when I ran this from SQL View Access still prompted me for the parameter diff(yd).

    although a noob I have looked online long enough to have learned that if you are being prompted for an inappropriate parameter then there is usually something wrong with the syntax, but I just can't see what I've done wrong.

    appreciate any help you can offer.


    thank you.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    diff must be a function in Access.
    And unless you built it , it does not exist in Access.


  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    [diff(yd)] appears to be a fieldname using punctuation/special characters. Advise not to do that with exception of underscore. Better would be diff_yd or DiffYd.

    [delta] and [days] are calculated fields. You will have to repeat those calculations in the ORDER BY expression as well.

    Alternately, build another query that uses the first one as data source and apply sort in the second query.

    Or build a report and use its Sorting & Grouping functionality.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    Code:
    Order By (Year([tbl_400m.dat])-Year([tbl_athlete.birthdate])-1))+(IIf([tbl_400m.dat]-[ran(b)]>0,[tbl_400m.dat]-[ran(b)],365+[tbl_400m.dat]-[ran(b)])  / 365)
    As June7 said, you will have to repeat the calculations in the order by clause.
    I've never repeated one so complex as this, so good luck!

  5. #5
    Cottonshirt is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2018
    Posts
    116
    thank you very much.

    I used the advice from June7 to change field names to include only the underscore (diff(yd) becomes diff_yd, etc) and also the suggestion to build a second query that uses the first as its data source.

    for the benefit of anyone else with the same problem, the SQL now looks like this:

    SELECT [tbl_400m.dat] AS dat, [tbl_athlete.fname]+" "+[tbl_athlete.sname]+" ("+[tbl_athlete.natcode]+") " AS name, tbl_athlete.birthdate, DateSerial(Year([tbl_400m.dat]),Month([tbl_athlete.birthdate]),Day([tbl_athlete.birthdate])) AS ran_b, IIf([tbl_400m.dat]-[ran_b]>0,Year([tbl_400m.dat])-Year([tbl_athlete.birthdate]),Year([tbl_400m.dat])-Year([tbl_athlete.birthdate])-1) AS delta, IIf([tbl_400m.dat]-[ran_b]>0,[tbl_400m.dat]-[ran_b],365+[tbl_400m.dat]-[ran_b]) AS days, (IIf([tbl_400m.dat]-[ran_b]>0,Year([tbl_400m.dat])-Year([tbl_athlete.birthdate]),Year([tbl_400m.dat])-Year([tbl_athlete.birthdate])-1))+((IIf([tbl_400m.dat]-[ran_b]>0,[tbl_400m.dat]-[ran_b],365+[tbl_400m.dat]-[ran_b]))/365) AS diff_yd, ([perf]*86400) AS [time], Format([delta],"00")+" years, "+Format([days],"0")+" days" AS age_m
    FROM (tbl_400m INNER JOIN tbl_athlete ON (tbl_400m.fname = tbl_athlete.fname) AND (tbl_400m.sname = tbl_athlete.sname)) INNER JOIN tbl_series ON tbl_400m.cha = tbl_series.cha
    WHERE (((tbl_athlete.birthdate) Is Not Null) AND ((tbl_400m.medal) Is Not Null))
    ORDER BY [tbl_athlete.fname]+" "+[tbl_athlete.sname]+" ("+[tbl_athlete.natcode]+") ";

    since the second query does the sorting there isn't any need to have this sorted at all, which resolves the complexity suggested by davegri.

    and, in case anyone is interested, the youngest medallist in the 400m is Nikolay Chernyetskiy (URS), who took a bronze medal at the European Indoor Championships in Milan, 1978 (46.72) at the age of 18 years 113 days.

    thank you all very much for your help.

  6. #6
    Bh89 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    3
    Hi All,

    I have very minimal experience on Access; and I would like to know how to sort a calculated field in a report.
    Coloum name: Weeks since Ref Recvd (which is a calculated field using =DateDiff("ww",[Ref Recvd],Now()).
    This is a waiting list I am building, an I need to be able to sort this coloum, which has the arrows greyed out when i try to do this.

    I have also tried moving the calculated field to the query I am referencing however it does not seem to update the report when new data is added to the report.

    If there is any solution to this I would appreciated advice.

    Please let me know if there is any more information you need.

    Any help would be appreciated.

    Thanks

    BH89

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    BH, instead of tacking on your request to an existing thread, you should start a new thread for your problem.
    This thread has been tagged as SOLVED a couple of months ago.

    Have you looked at "sorting and grouping" in the report design view on the ribbon?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Report in PrintPreview is not dynamic. Have to close and reopen to refresh. Report in ReportView can be refreshed by clicking RefreshAll on ribbon.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query Sort by Field Value
    By Thom in forum Queries
    Replies: 3
    Last Post: 08-16-2017, 09:33 AM
  2. Sort query a by a calculated control
    By gg80 in forum Queries
    Replies: 4
    Last Post: 10-01-2015, 05:40 PM
  3. Replies: 2
    Last Post: 12-30-2014, 01:32 PM
  4. Select Query after Sort
    By kdilag in forum Queries
    Replies: 5
    Last Post: 01-17-2012, 09:50 AM
  5. Sort Calculated Ages in to Age Groups
    By l3111 in forum Queries
    Replies: 2
    Last Post: 06-09-2011, 04:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums