Results 1 to 7 of 7
  1. #1
    SiebeD is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    14

    Dates won't sort by date (query sql)

    Hey everyone!

    I have a list of data that gives me a value every 15 min. I want to sort this data in a Query but then i want the sum of the 4 values of that hour. I am trying to do this with this SQL code:

    Code:
     
    SELECT (Format([KwartTijd],'ddddd hh')) AS DATUM, Sum(PS_Hautrage.Vermogen) AS SomHautrage, Sum(PS_Buggenhout.Vermogen) AS SomBuggenhout, Sum(PS_Mainvault.Vermogen) AS SomMainvault, Sum(PS_AssePastinaken.Vermogen) AS SomAssePastinaken, Sum(PS_BrakelKerkhofStrt.Vermogen) AS SomBrakelKerkhofstraat, Sum(PS_AthChaussee.Vermogen) AS SomAthChaussee
    
    
    FROM ((PS_AthChaussee INNER JOIN PS_BrakelKerkhofStrt ON PS_AthChaussee.Id = PS_BrakelKerkhofStrt.Id) INNER JOIN PS_AssePastinaken ON (PS_AthChaussee.Id = PS_AssePastinaken.Id) AND (PS_AthChaussee.Id = PS_AssePastinaken.Id)) INNER JOIN (((DatumTijd INNER JOIN PS_Hautrage ON DatumTijd.Id = PS_Hautrage.Id) INNER JOIN PS_Buggenhout ON (PS_Hautrage.Id = PS_Buggenhout.Id) AND (PS_Hautrage.Id = PS_Buggenhout.Id) AND (PS_Hautrage.Id = PS_Buggenhout.Id) AND (DatumTijd.Id = PS_Buggenhout.Id)) INNER JOIN PS_Mainvault ON (PS_Hautrage.Id = PS_Mainvault.Id) AND (PS_Buggenhout.Id = PS_Mainvault.Id) AND (DatumTijd.Id = PS_Mainvault.Id)) ON (PS_BrakelKerkhofStrt.Id = DatumTijd.Id) AND (PS_AthChaussee.Id = DatumTijd.Id) AND (PS_AssePastinaken.Id = DatumTijd.Id) AND (PS_AthChaussee.Id = PS_Buggenhout.Id) AND (PS_BrakelKerkhofStrt.Id = PS_Buggenhout.Id) AND (PS_AssePastinaken.Id = PS_Buggenhout.Id) AND (PS_AssePastinaken.Id = PS_Hautrage.Id) AND (PS_AthChaussee.Id = PS_Hautrage.Id) AND (PS_BrakelKerkhofStrt.Id = PS_Hautrage.Id) AND (PS_AssePastinaken.Id = PS_Hautrage.Id) AND (PS_BrakelKerkhofStrt.Id = PS_Mainvault.Id) AND (PS_AssePastinaken.Id = PS_Mainvault.Id) AND (PS_AthChaussee.Id = PS_Mainvault.Id)
    
    GROUP BY (Format([KwartTijd],'ddddd hh'));
    The problem is that these values wont sort by date if i do this.
    And after this i need to calculate other data (average of a month, a week or a day) to display this on a Graph. I think the best way to do this is is from this query that i am trying to make by hour. But i think this wont work because the dates wont sort correct in this query.

    Thanks!

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    That sql statement is quite a mouthful!

    Shot in the dark: have you tried saving this query as a subquery, then calling it from another query and sorting it there?

    Here's some "pretty" formatting in case someone wants to take a crack at it:

    Code:
     SELECT ( Format([KwartTijd], 'ddddd hh') ) AS DATUM, 
           Sum(PS_Hautrage.Vermogen)           AS SomHautrage, 
           Sum(PS_Buggenhout.Vermogen)         AS SomBuggenhout, 
           Sum(PS_Mainvault.Vermogen)          AS SomMainvault, 
           Sum(PS_AssePastinaken.Vermogen)     AS SomAssePastinaken, 
           Sum(PS_BrakelKerkhofStrt.Vermogen)  AS SomBrakelKerkhofstraat, 
           Sum(PS_AthChaussee.Vermogen)        AS SomAthChaussee 
    FROM   ((PS_AthChaussee 
             INNER JOIN PS_BrakelKerkhofStrt 
                     ON PS_AthChaussee.Id = PS_BrakelKerkhofStrt.Id) 
            INNER JOIN PS_AssePastinaken 
                    ON ( PS_AthChaussee.Id = PS_AssePastinaken.Id ) 
                       AND ( PS_AthChaussee.Id = PS_AssePastinaken.Id )) 
           INNER JOIN (((DatumTijd 
                         INNER JOIN PS_Hautrage 
                                 ON DatumTijd.Id = PS_Hautrage.Id) 
                        INNER JOIN PS_Buggenhout 
                                ON ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                   AND ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                   AND ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                   AND ( DatumTijd.Id = PS_Buggenhout.Id )) 
                       INNER JOIN PS_Mainvault 
                               ON ( PS_Hautrage.Id = PS_Mainvault.Id ) 
                                  AND ( PS_Buggenhout.Id = PS_Mainvault.Id ) 
                                  AND ( DatumTijd.Id = PS_Mainvault.Id )) 
                   ON ( PS_BrakelKerkhofStrt.Id = DatumTijd.Id ) 
                      AND ( PS_AthChaussee.Id = DatumTijd.Id ) 
                      AND ( PS_AssePastinaken.Id = DatumTijd.Id ) 
                      AND ( PS_AthChaussee.Id = PS_Buggenhout.Id ) 
                      AND ( PS_BrakelKerkhofStrt.Id = PS_Buggenhout.Id ) 
                      AND ( PS_AssePastinaken.Id = PS_Buggenhout.Id ) 
                      AND ( PS_AssePastinaken.Id = PS_Hautrage.Id ) 
                      AND ( PS_AthChaussee.Id = PS_Hautrage.Id ) 
                      AND ( PS_BrakelKerkhofStrt.Id = PS_Hautrage.Id ) 
                      AND ( PS_AssePastinaken.Id = PS_Hautrage.Id ) 
                      AND ( PS_BrakelKerkhofStrt.Id = PS_Mainvault.Id ) 
                      AND ( PS_AssePastinaken.Id = PS_Mainvault.Id ) 
                      AND ( PS_AthChaussee.Id = PS_Mainvault.Id ) 
    GROUP  BY ( Format([KwartTijd], 'ddddd hh') );  

  3. #3
    SiebeD is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    14
    Quote Originally Posted by kd2017 View Post
    That sql statement is quite a mouthful!

    Shot in the dark: have you tried saving this query as a subquery, then calling it from another query and sorting it there?

    Here's some "pretty" formatting in case someone wants to take a crack at it:

    Code:
     SELECT ( Format([KwartTijd], 'ddddd hh') ) AS DATUM, 
           Sum(PS_Hautrage.Vermogen)           AS SomHautrage, 
           Sum(PS_Buggenhout.Vermogen)         AS SomBuggenhout, 
           Sum(PS_Mainvault.Vermogen)          AS SomMainvault, 
           Sum(PS_AssePastinaken.Vermogen)     AS SomAssePastinaken, 
           Sum(PS_BrakelKerkhofStrt.Vermogen)  AS SomBrakelKerkhofstraat, 
           Sum(PS_AthChaussee.Vermogen)        AS SomAthChaussee 
    FROM   ((PS_AthChaussee 
             INNER JOIN PS_BrakelKerkhofStrt 
                     ON PS_AthChaussee.Id = PS_BrakelKerkhofStrt.Id) 
            INNER JOIN PS_AssePastinaken 
                    ON ( PS_AthChaussee.Id = PS_AssePastinaken.Id ) 
                       AND ( PS_AthChaussee.Id = PS_AssePastinaken.Id )) 
           INNER JOIN (((DatumTijd 
                         INNER JOIN PS_Hautrage 
                                 ON DatumTijd.Id = PS_Hautrage.Id) 
                        INNER JOIN PS_Buggenhout 
                                ON ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                   AND ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                   AND ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                   AND ( DatumTijd.Id = PS_Buggenhout.Id )) 
                       INNER JOIN PS_Mainvault 
                               ON ( PS_Hautrage.Id = PS_Mainvault.Id ) 
                                  AND ( PS_Buggenhout.Id = PS_Mainvault.Id ) 
                                  AND ( DatumTijd.Id = PS_Mainvault.Id )) 
                   ON ( PS_BrakelKerkhofStrt.Id = DatumTijd.Id ) 
                      AND ( PS_AthChaussee.Id = DatumTijd.Id ) 
                      AND ( PS_AssePastinaken.Id = DatumTijd.Id ) 
                      AND ( PS_AthChaussee.Id = PS_Buggenhout.Id ) 
                      AND ( PS_BrakelKerkhofStrt.Id = PS_Buggenhout.Id ) 
                      AND ( PS_AssePastinaken.Id = PS_Buggenhout.Id ) 
                      AND ( PS_AssePastinaken.Id = PS_Hautrage.Id ) 
                      AND ( PS_AthChaussee.Id = PS_Hautrage.Id ) 
                      AND ( PS_BrakelKerkhofStrt.Id = PS_Hautrage.Id ) 
                      AND ( PS_AssePastinaken.Id = PS_Hautrage.Id ) 
                      AND ( PS_BrakelKerkhofStrt.Id = PS_Mainvault.Id ) 
                      AND ( PS_AssePastinaken.Id = PS_Mainvault.Id ) 
                      AND ( PS_AthChaussee.Id = PS_Mainvault.Id ) 
    GROUP  BY ( Format([KwartTijd], 'ddddd hh') );  
    How can you sort the data in another query?

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Again, this is just a shot in the dark. I don't know if this will fix your issue.
    You can wrap your first query in parentheses making it a subquery and 'select' it again like this:
    Code:
    SELECT * 
    FROM   (SELECT ( Format([KwartTijd], 'ddddd hh') ) AS DATUM, 
                   Sum(PS_Hautrage.Vermogen)           AS SomHautrage, 
                   Sum(PS_Buggenhout.Vermogen)         AS SomBuggenhout, 
                   Sum(PS_Mainvault.Vermogen)          AS SomMainvault, 
                   Sum(PS_AssePastinaken.Vermogen)     AS SomAssePastinaken, 
                   Sum(PS_BrakelKerkhofStrt.Vermogen)  AS SomBrakelKerkhofstraat, 
                   Sum(PS_AthChaussee.Vermogen)        AS SomAthChaussee 
            FROM   ((PS_AthChaussee 
                     INNER JOIN PS_BrakelKerkhofStrt 
                             ON PS_AthChaussee.Id = PS_BrakelKerkhofStrt.Id) 
                    INNER JOIN PS_AssePastinaken 
                            ON ( PS_AthChaussee.Id = PS_AssePastinaken.Id ) 
                               AND ( PS_AthChaussee.Id = PS_AssePastinaken.Id )) 
                   INNER JOIN (((DatumTijd 
                                 INNER JOIN PS_Hautrage 
                                         ON DatumTijd.Id = PS_Hautrage.Id) 
                                INNER JOIN PS_Buggenhout 
                                        ON ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                           AND ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                           AND ( PS_Hautrage.Id = PS_Buggenhout.Id ) 
                                           AND ( DatumTijd.Id = PS_Buggenhout.Id )) 
                               INNER JOIN PS_Mainvault 
                                       ON ( PS_Hautrage.Id = PS_Mainvault.Id ) 
                                          AND ( PS_Buggenhout.Id = PS_Mainvault.Id ) 
                                          AND ( DatumTijd.Id = PS_Mainvault.Id )) 
                           ON ( PS_BrakelKerkhofStrt.Id = DatumTijd.Id ) 
                              AND ( PS_AthChaussee.Id = DatumTijd.Id ) 
                              AND ( PS_AssePastinaken.Id = DatumTijd.Id ) 
                              AND ( PS_AthChaussee.Id = PS_Buggenhout.Id ) 
                              AND ( PS_BrakelKerkhofStrt.Id = PS_Buggenhout.Id ) 
                              AND ( PS_AssePastinaken.Id = PS_Buggenhout.Id ) 
                              AND ( PS_AssePastinaken.Id = PS_Hautrage.Id ) 
                              AND ( PS_AthChaussee.Id = PS_Hautrage.Id ) 
                              AND ( PS_BrakelKerkhofStrt.Id = PS_Hautrage.Id ) 
                              AND ( PS_AssePastinaken.Id = PS_Hautrage.Id ) 
                              AND ( PS_BrakelKerkhofStrt.Id = PS_Mainvault.Id ) 
                              AND ( PS_AssePastinaken.Id = PS_Mainvault.Id ) 
                              AND ( PS_AthChaussee.Id = PS_Mainvault.Id ) 
            GROUP  BY ( Format([KwartTijd], 'ddddd hh') )) AS sbqry 
    ORDER  BY DATUM;  
    or you could save your first query and call it from another query like this:
    Code:
    SELECT * 
    FROM saved_subquery_name 
    ORDER  BY DATUM;  
    My reasoning for this is you have a crazy FROM clause there with all those JOINs/ANDs. I wonder if access is just having a hard time processing and sorting. This way you force access to collect the data FIRST in a subquery, and then sorting them second after all the hard work had been done. I've found sometimes the access sql engine needs some hand holding when the sql start to get complex.

    I could be wrong but I believe that if you save your subqueries separately (like example 2) it's supposed to be better for performance.

    (edit)
    fyi, it depends on the circumstances but if you can get away with it I think it's generally recommended not to do the sorting in the query itself and just sort it in the form or report instead.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Does KwartTijd include a Time component?

    If so, and this also just a guess (I don't use SQL extensively,) but when you use

    Format([KwartTijd],'ddddd hh')

    your formatting changes KwartTijd from a Date/Time Datatype to a String...that's what the Format() function does. And the 'Group By' wouldn't group Strings that the same way as grouping by Date/Time.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    SiebeD is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    14
    Quote Originally Posted by Missinglinq View Post
    Does KwartTijd include a Time component?

    If so, and this also just a guess (I don't use SQL extensively,) but when you use

    Format([KwartTijd],'ddddd hh')

    your formatting changes KwartTijd from a Date/Time Datatype to a String...that's what the Format() function does. And the 'Group By' wouldn't group Strings that the same way as grouping by Date/Time.

    Linq ;0)>
    O well thats possible indeed because the code from @kd2017 won't work either. And this would explain why.
    But is it possible to sort the dates then? Now I am looking into the function 'FormatDateTim()' but here i dont have the option to change the time from every 15 mins to every Hour.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    I would sort by the dates. Use functions like Year(), Month(), Day() and Hour() to get the relevant parts for your summing.?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 1
    Last Post: 08-30-2016, 06:04 AM
  2. Multi table Date query to a Between dates frm
    By Bomberboy67 in forum Queries
    Replies: 1
    Last Post: 12-16-2015, 07:44 PM
  3. Replies: 5
    Last Post: 08-14-2015, 02:53 PM
  4. Replies: 10
    Last Post: 05-09-2014, 10:54 AM
  5. Replies: 1
    Last Post: 12-09-2013, 05:57 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