Results 1 to 4 of 4
  1. #1
    Thom is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    2

    Question Query Sort by Field Value

    Need to sort a query based on a text field, and not in alphabetical order. In the datasheet for the table below, I need to return everything sorted by the Part_No. However not in alphabetical order by part number. First those which start with "L", then the others can be in alphabetical order - just need all the "L" part numbers first and ordered by Part_No. Here is some hypothetical sample data:



    EQUIP
    Part_No Location Version
    LN-23yy AL A
    FE-R72g AL G
    LN-734r AL B
    SC-003g AL C
    VR-Z782 AL A
    FS-F655 NY B
    LN-447s NY C














    This query will be used in a report, along with a subquery (which is straight forward with no problems creating). Was trying to do something with a LIKE statement in the WHERE clause, but that doesn't process correctly. Obviously cannot put ASC or DSC in a SORT BY clause for the PART_NO as the required order is not alphabetical. Please know that I am fairly new to MS Access, and honestly am not certain exactly how to do this. Sounds simple enough, but I've been working on it for many hours already.

    Then thought maybe use a case statement, but this too does not work as it returns all the values with the WHERE clause (had to put the case in an ORDER BY clause to not error). Insights and help are very welcome!!

    SELECT part_no, location, version
    FROM equip
    WHERE location = "AL"
    ORDER BY CASE WHEN part_no LIKE "L*" THEN 1
    WHEN part_no LIKE "F*" THEN 2
    WHEN part_no LIKE "S*" THEN 3
    WHEN part_no LIKE "V*" THEN 4 END ASC;

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    case when is sql server - is that what you are using?

    The access equivalent is the switch function, but might be slow for a large dataset

    If access I would instead try

    Code:
    SELECT part_no, location, version
     FROM equip
     WHERE location = "AL"
     ORDER BY iif(part_no LIKE "L*",0,1), Part_No
    or just

    Code:
    SELECT part_no, location, version
     FROM equip
     WHERE location = "AL"
     ORDER BY part_no LIKE "L*", Part_No

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If you are in SQL Server, I'd make a field out of the CASE statement and sort it in the form/report it's used for. That said, if in SQL Server you need to use single quotes and % as the wildcard:

    CASE WHEN part_no LIKE 'L%' THEN 1
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Thom is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    2
    Thank you, using the iif statement works great! Like I said, I'm new to Access and did not know about this. My background is with Oracle and Oracle Application Express (APEX), so it's been an interesting learning curve.

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

Similar Threads

  1. Replies: 6
    Last Post: 04-04-2017, 11:09 AM
  2. Replies: 7
    Last Post: 01-19-2016, 05:28 PM
  3. How do I do a subtotal on a sort field?
    By bmarkj in forum Reports
    Replies: 3
    Last Post: 07-09-2014, 09:36 AM
  4. Unable to sort field
    By Lookup in forum Access
    Replies: 5
    Last Post: 08-18-2013, 01:20 PM
  5. Replies: 3
    Last Post: 04-19-2011, 06:41 PM

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