Results 1 to 7 of 7
  1. #1
    seb is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    73

    SQL code to group records with successive dates in a row

    Hello. For a query, I have this SQL code that works well.

    Code:
    SELECT Min(tblmod.dates) AS date1, Max(tblmod.dates) AS date2, tbltaks.tbltaskname, Format(Sqr([Qty]),"Standard") AS rc, tblmod.plot_idFROM tbltaks INNER JOIN (tblplot INNER JOIN tblmod ON tblplot.plotid = tblmod.plot_id) ON tbltaks.task_id = tblmod.task_id
    GROUP BY tbltask.tbltaskname, tblmod.dates, tblmod.plot_id
    HAVING (((tblmod.plot_id)=54))
    ORDER BY tblmod.dates;
    However, I am looking for an SQL code to group tasks stored in successive dates into a single row.
    I have been searching the web but I don't know type the correct keywords.
    In the attached image:
    The table on the left is my SQL code.


    The table on the right is the one I need to program.

    I really appreciate your help.Click image for larger version. 

Name:	sql.jpg 
Views:	19 
Size:	293.9 KB 
ID:	39418

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I am not understanding the right side output. There is no task6 - I think the second task5 should be task6. Why are the two task3 records combined to one row and task2 records are not? What is rc?

    You need explain the rules for this data aggregation. If you were to do this analysis manually, what decision steps would you follow?

    Show original raw data. If you want to provide db for analysis, follow instructions at bottom of my post.
    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.

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    looks like you have free typed your code - there appear to be typos (highlighted red) which would prevent the code from running. Best to just copy/paste what you have. You should only need to makes some small changes, try this

    Code:
    SELECT Min(tblmod.dates) AS date1, Max(tblmod.dates) AS date2, tbltaks.tbltaskname, Format(sum(Sqr([Qty])),"Standard") AS rc FROM tbltaks INNER JOIN (tblplot INNER JOIN tblmod ON tblplot.plotid = tblmod.plot_id) ON tbltaks.task_id = tblmod.task_id
    WHERE (((tblmod.plot_id)=54))
    GROUP BY tbltask.tbltaskname
    ORDER BY tblmod.dates;
    I have removed fields which appear to not be required, changed the HAVING to WHERE and summed the Qty before you format it. Note that best practice is to format values in a form or report, not in the underlying query

  4. #4
    seb is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    73
    Many thanks for your reply
    June I give you my apologies for the errors you indicate. As Ajax said, I typed the SQL code.
    June and Ajax. I want you to briefly explain what I’m trying to do.
    I have a form with a textbox for date1 and another textbox for date2 and a combobox for selecting the plot number. Then a command button exports the query to Excel.
    This is my copy and paste SQL code:
    SELECT Min(tblmod.date) AS date1, Max(tblmod.date) AS date2, tbltaskname.taskname, Format(Sqr([hours]),"Standard") AS rc, Format(Sum([quantity]*8),"Standard") AS hours
    FROM tbltaskname INNER JOIN (tblplot INNER JOIN tblmod ON tblplot.plot_id = tblmod.plot_id) ON tbltaskname.task_id = tblmod.task_id
    GROUP BY tbltaskname.taskname, tblmod.plot_id, tblmod.date
    ORDER BY Min(tblmod.date);
    Here is the query:
    date1 date2 taskname rc hours
    18/12/2017 18/12/2017 task1 6,32 40,00
    19/12/2017 19/12/2017 task1 6,32 40,00
    20/12/2017 20/12/2017 task1 6,32 40,00
    21/12/2017 21/12/2017 task1 6,32 40,00
    11/01/2018 11/01/2018 task2 2,83 8,00
    15/01/2018 15/01/2018 task2 4,00 16,00
    15/01/2018 15/01/2018 task3 13,56 184,00
    16/01/2018 16/01/2018 task3 14,97 224,00
    16/01/2018 16/01/2018 task2 4,90 24,00
    29/01/2018 29/01/2018 task2 2,83 8,00
    21/06/2018 21/06/2018 task2 4,90 24,00
    22/06/2018 22/06/2018 task2 4,90 24,00
    21/06/2018 21/06/2018 task4 6,32 40,00
    22/06/2018 22/06/2018 task5 4,90 24,00
    26/06/2018 26/06/2018 task5 10,95 120,00
    26/06/2018 26/06/2018 task2 2,83 8,00
    28/06/2018 28/06/2018 task2 2,83 8,00

    I’ve been looking for an SQL query code to displays the records like this:
    date1 date2 taskname rc hours
    18/12/2017 21/12/2017 task1 25,28 160,00
    15/01/2018 16/01/2018 task3 28,53 408,00
    21/06/2018 22/06/2018 task2 9,80 48,00
    21/06/2018 21/06/2018 task4 6,32 40,00
    22/06/2018 22/06/2018 task5 4,90 24,00
    26/06/2018 26/06/2018 task5 10,95 120,00
    26/06/2018 26/06/2018 task2 2,83 8,00
    28/06/2018 28/06/2018 task2 2,83 8,00

    I spent time searched google many hours about this, but not sure the keywords to use.
    Please help.
    Last edited by seb; 08-13-2019 at 10:52 PM. Reason: Improve the position of rows and columns

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Need your raw data, not results of query you don't like.

    If I understand what you mean by 'successive', I don't see way to do this with query alone. I would probably use VBA writing records to temp table.
    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.

  6. #6
    seb is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2010
    Posts
    73
    June. Thanks for your reply.
    I am preparing my MS Relational DB file to send it to you.
    Cheers

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    seb.zip

    Example database using your data, though your output does not match what it should be I think.

    I just added one additional field for sequencing within a particular task then just ran an aggregate query on the data in each sequence.

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

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2016, 01:07 PM
  2. Replies: 5
    Last Post: 10-04-2016, 11:56 AM
  3. Replies: 30
    Last Post: 06-16-2015, 08:37 AM
  4. Code: Group and select first records
    By cfobare79 in forum Access
    Replies: 9
    Last Post: 02-10-2015, 04:08 PM
  5. Dates by Group?
    By coliver in forum Reports
    Replies: 2
    Last Post: 11-07-2009, 09:03 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