Results 1 to 9 of 9
  1. #1
    mitchbvi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Nov 2014
    Posts
    5

    Sort a subset within a query

    I am trying to figure out how to sort a subset in a query. I have three fields BatchID,Month and Year. The BatchID can have multiple months and years. I would like to order the final sort so as the Batchs appeared in the order of the highest date combination with in each Batch. Hopefully the following table explains this.
    Click image for larger version. 

Name:	Screen Shot 2014-11-24 at 6.21.44 PM.png 
Views:	17 
Size:	11.0 KB 
ID:	18841

    The following is the SQL of my first Query which establish's the lowest and highest date in each batch.

    Code:
    SELECT tblRevOnlySelYear.BatchID, tblRevOnlySelYear.OperatorID, Max([Year] & IIf(Len([Month])=1,0 & [Month],[Month])) AS Expr1FROM tblRevOnlySelYear
    GROUP BY tblRevOnlySelYear.BatchID, tblRevOnlySelYear.OperatorID
    HAVING (((tblRevOnlySelYear.OperatorID)=1))
    ORDER BY Max([Year] & IIf(Len([Month])=1,0 & [Month],[Month]));
    The next SQL is the second query which results in the Batchs in the correct order by year but the months are not correct.



    Code:
    SELECT tblRevOnlySelYear.BatchID, tblRevOnlySelYear.OperatorID, tblRevOnlySelYear.Month, tblRevOnlySelYear.Year, qryRevTest4_1.[1stDate], qryRevTest4_1.LastDateFROM qryRevTest4 AS qryRevTest4_1 INNER JOIN tblRevOnlySelYear ON qryRevTest4_1.BatchID = tblRevOnlySelYear.BatchID
    WHERE (((tblRevOnlySelYear.OperatorID)=1))
    ORDER BY qryRevTest4_1.LastDate;
    Click image for larger version. 

Name:	Screen Shot 2014-11-24 at 6.19.02 PM.png 
Views:	17 
Size:	18.2 KB 
ID:	18840

    Sorry about the size of the files.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    I hope your MONTH & YEAR are numeric, otherwise the sorts wont work, But you made LASTDATE field which will,
    so the sort is:
    order by BATCHID ,LASTDATE

    BUT
    if you only want 1 date (the max) for each batch,
    select BATCHID, max(LASTDATE) as MaxDate from table

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    It's not clear what you want to sort by, but the ORDER BY can contain as many fields as you need for the sort you require - they don't have to be combined into one expression. You can have some on ascending order (the default), and some on descending order.

    So, if you wanted the batches in ascending order, and the dates in descending order within each batch, you could use:

    ORDER BY BatchID, Year DESC, Month DESC for the first table, and

    ORDER BY BatchID, LastDate DESC for the second one.

    Just remember that the order you specify the fields in the ORDER BY is important.

    John

  4. #4
    mitchbvi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Nov 2014
    Posts
    5
    Thanks for taking the time to respond unfortunately the one thing I do not want to do is sort by BatchID, I guess I did not explain my self very well. What I need is to sort with in the BatchID month and year in ascending order. Then I want to sort the whole batchID groups by the highest date . In the result that I showed in my original post you will see that the BatchID's are not sequential and are in order as far as the year is concerned. However they are not sorted in order for the month, the first two batches in the example are correct but batch 4887 is not with month 3 appearing before month 2.

    Peter

  5. #5
    mitchbvi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Nov 2014
    Posts
    5
    Hi John

    Thank you for taking the time to reply. I have replied to ranman post and I hope it now explains what I am trying to achieve. In short form I want each batch sorted by year and month in ascending order and I then want each batch sorted in the highest date in each batch. My example does that for the year but not for the month.

    Thanks again the help is greatly appreciated.

    Peter

  6. #6
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    You keep saying sort by Batch , yet you DONT want to sort by batchID.
    ??!!

  7. #7
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Try ORDER BY lastdate, batchID, Month, Year

    This assumes that every record for ony one BatchID has the same LastDate.

    You are going to have to sort by BatchID somewhere, otherwise there is no guarantee that all records for the same batch will be kept together.

    John

  8. #8
    mitchbvi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Nov 2014
    Posts
    5
    My Apologies for the lack of clarity.
    First I need to sort within each Batch by year and month in ascending order for both month and year.
    Then I need each batch sorted not by the batchID but by the highest date within each batch, hope that makes it clearer.

    Thanks again

    Peter

  9. #9
    mitchbvi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Nov 2014
    Posts
    5
    Yes I need two queries one that establishes what the last date is for each batch and that was the first SQL in the original post . that is the last sort order so the object is to keep the records with in each batch together and you have given me the clue I needed. I have now concatenated ( lastdate ,batchid,year,month) and that looks Ok just need to test it on the full DB.

    Cannot believe something so simple has taken me so long, started to write code to sort recordsets made up of each batch and got nowhere with that, talk about Occam's Razor.

    Thanks again

    Peter

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

Similar Threads

  1. Replies: 1
    Last Post: 04-07-2014, 02:59 AM
  2. Query: Add 'Subset' counter to records
    By JangLang in forum Access
    Replies: 1
    Last Post: 09-20-2013, 10:51 AM
  3. Replies: 3
    Last Post: 01-15-2013, 01:58 PM
  4. Print Subset of Records from From
    By ssthornton in forum Programming
    Replies: 6
    Last Post: 05-21-2012, 02:28 PM
  5. Replies: 5
    Last Post: 05-05-2012, 09:58 PM

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