Results 1 to 6 of 6
  1. #1
    abordeau is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Oct 2013
    Posts
    8

    Help with query consonidating

    Hi all,

    I have a db that has grown quite large. I am trying to gather 3 month, 6 month, and historical data from a table for 16 pieces of equipment called "final equipment" Then for some of the equipment I need the same data (plus additional data) for each component of each piece. You can imagine how large the full db is. Now access is having a nervous breakdown and telling me that it can't open anymore db's. (I'm assuming because it's to large.)

    Currently I need 5 queries to get the results I need. One for 3 month, one for 6 month, one for historical, one to join, and one for a final (cleaned up). Multiply that by 16 + and you get a motherlode of queries.

    Can someone kindly look at my attachment and see if they can help me reduce the amount of queries needed for each result?

    Use my date selector. For consistency use the dates 7/1 and 9/30 for 3 months and 4/1 and 9/30 for 6 months.

    Thanks for any help in advance.
    Practice 1.accdbPractice.zip
    Angelina

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Code:
    Current Query: Mech Final Equip 3 Mth
    SELECT DISTINCTROW 
       Format$(T1.[Up Date/Time],'yyyy mm') AS [Up Date/Time By Month], 
       T1.[Final Equipment] AS Expr1, 
       Sum(T1.Down) AS [Sum Of Down], 
       Count(*) AS [Count Of 416 Data]
    FROM [416 Data] AS T1
    GROUP BY 
       Format$([416 Data].[Up Date/Time],'yyyy mm'), 
       T1.[Final Equipment], 
       T1.Status, 
       T1.Reason, 
       Year(T1.[Up Date/Time])*12+DatePart('m',T1.[Up Date/Time])-1
    HAVING 
    (((Format$([416 Data].[Up Date/Time],'yyyy mm')) 
    Between (Format([Forms]![H36 Date Selector]![Start Date 1],'yyyy mm')) 
    And (Format([Forms]![H36 Date Selector]![End Date 1],"yyyy mm"))) 
    AND (([416 Data].[Final Equipment]) Is Not Null) 
    AND (([416 Data].[Status])="brkdwn") 
    AND (([416 Data].[Reason])="mech"));
    1) DISTINCT is only needed because you have extra levels in the GROUP BY that aren't broken out in the SELECT. Since you don't actually need the results broken out by status and reason, since only one type of each will be returned, you can remove them from GROUP BY clause, and kill the DISTINCT keyword.

    2) The numeric calc for the year/month in the GROUP BY isn't needed, because "YYYY MM" format yields the same effective result.
    3) No reason to rename [Final Equipment]. That's probably left over from testing different versions.

    4) Depending on Jet's optimization algorythms, it can be more efficient to move the HAVING conditions to a WHERE clause for any conditions where the HAVING can be determined at record selection time.

    5) I generally like to count something specific rather than count *. I can't swear from knowledge of Jet's internals that it's always faster, but my experience tells me that at least sometimes it is.

    6) I always alias my tables to save typing and make the SQl easier to read.

    Based on those, the following is a more optimized version of [Mech Final Equip 3 Mth]
    Code:
    Partially Optimized Query: Mech Final Equip 3 Mth
    SELECT 
       Format$(T1.[Up Date/Time],'yyyy mm') AS [Up Date/Time By Month], 
       T1.[Final Equipment], 
       Sum(T1.Down) AS [Sum Of Down], 
       Count(T1.Down) AS [Count Of 416 Data]
    FROM [416 Data] AS T1
    WHERE (((T1.[Final Equipment]) Is Not Null) 
       AND ((T1.[Status])="brkdwn") 
       AND ((T1.[Reason])="mech"))
       AND (        (Format$(T1.[Up Date/Time],'yyyy mm')) 
            BETWEEN (Format([Forms]![H36 Date Selector]![Start Date 1],'yyyy mm')) 
                AND (Format([Forms]![H36 Date Selector]![End Date 1],"yyyy mm"))
          )) 
    GROUP BY 
       Format$(T1.[Up Date/Time],'yyyy mm'), 
       T1.[Final Equipment];
    One more optimization is available, and I'd strongly suggest you use it. You are currently calculating every single 'yyyy mm' at the record level BEFORE you exclude all the records that are out of the date range. Just by NOT changing the format of the date field before that condition test, you should achieve a massive increase in speed.
    Code:
    Optimized Query: Mech Final Equip 3 Mth
    SELECT 
       Format$(T1.[Up Date/Time],'yyyy mm') AS [Up Date/Time By Month], 
       T1.[Final Equipment], 
       Sum(T1.Down) AS [Sum Of Down], 
       Count(*) AS [Count Of 416 Data]
    FROM [416 Data] AS T1
    WHERE (((T1.[Final Equipment]) Is Not Null) 
       AND ((T1.[Status])="brkdwn") 
       AND ((T1.[Reason])="mech")
       AND ((T1.[Up Date/Time]) 
             BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                 AND ([Forms]![H36 Date Selector]![End Date 1])
          )) 
    GROUP BY 
       Format$(T1.[Up Date/Time],'yyyy mm'), 
       T1.[Final Equipment];
    Last edited by Dal Jeanis; 11-11-2013 at 12:56 PM. Reason: fix parens in aircode

  3. #3
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    FYI - In a union, only the first table being UNIONed can give names to the query results, so all the later aliases are for documentation purposes only.

    Your end result can be achieved more efficiently without breaking the queries up and re-UNIONing them.

    The following query should get you one record per equipment per month, with columns for the 3-month, 6-month and historical filled out properly:
    Code:
    Optimized Query: Mech Final Equip 3 Mth
    SELECT 
       Format$(T1.[Up Date/Time],'yyyy mm') AS DownMonth, 
       T1.[Final Equipment] AS DownEquip, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                      AND ([Forms]![H36 Date Selector]![End Date 1]),
               T1.Down,
               0)) AS DownSum3Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                      AND ([Forms]![H36 Date Selector]![End Date 1]),
               1,
               0)) AS DownCount3Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 2]) 
                      AND ([Forms]![H36 Date Selector]![End Date 2]),
               T1.Down,
               0)) AS DownSum6Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 2]) 
                      AND ([Forms]![H36 Date Selector]![End Date 2]),
               1,
               0)) AS DownCount6Mos, 
       Sum(T1.Down) AS DownSumHist, 
       Count(T1.Down) AS DownCountHist
    FROM
        [416 Data] AS T1
    WHERE (
           ((T1.[Final Equipment]) Is Not Null) 
       AND ((T1.[Status])="brkdwn") 
       AND ((T1.[Reason])="mech"))
          )
    GROUP BY 
       Format$(T1.[Up Date/Time],'yyyy mm'), 
       T1.[Final Equipment];
    You will notice that for 3-month and 6-month counts I am summing either a 1 or 0, rather than counting the records.

    The following query should get you one record per equipment, with columns for the 3-month, 6-month and historical filled out properly:
    Code:
    SELECT 
       T1.[Final Equipment] AS DownEquip, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                      AND ([Forms]![H36 Date Selector]![End Date 1]),
               T1.Down,
               0)) AS DownSum3Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                      AND ([Forms]![H36 Date Selector]![End Date 1]),
               1,
               0)) AS DownCount3Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 2]) 
                      AND ([Forms]![H36 Date Selector]![End Date 2]),
               T1.Down,
               0)) AS DownSum6Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 2]) 
                      AND ([Forms]![H36 Date Selector]![End Date 2]),
               1,
               0)) AS DownCount6Mos, 
       Sum(T1.Down) AS DownSumHist, 
       Count(T1.Down) AS DownCountHist
    FROM 
       [416 Data] AS T1
    WHERE (
           ((T1.[Final Equipment]) Is Not Null) 
       AND ((T1.[Status])="brkdwn") 
       AND ((T1.[Reason])="mech"))
          )
    GROUP BY 
       T1.[Final Equipment];

  4. #4
    abordeau is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Oct 2013
    Posts
    8
    Thank you, I am currently trying your ideas in all my queries. I will let you know if Access decides to wig out again.

  5. #5
    abordeau is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Oct 2013
    Posts
    8

    Another problem

    First off let me say thanks for the above sql's. They are working just fine.

    This one however is giving me the Extra) in query expression 'sum(IIF([416 DATA].[up date/time]) BETWEEN ([forms]![H36 date selector]![start date 1]) And ([Forms]![H36 Date Selector]![End Date 1]), [416 DATA[.Down, 0)). With emphisis on the last parentheses. When I delete it it tells me that I have the wrong number of arguments used with the function in the query expression.

    What should I do?

    Quote Originally Posted by Dal Jeanis View Post
    FYI - In a union, only the first table being UNIONed can give names to the query results, so all the later aliases are for documentation purposes only.

    Your end result can be achieved more efficiently without breaking the queries up and re-UNIONing them.

    The following query should get you one record per equipment per month, with columns for the 3-month, 6-month and historical filled out properly:
    Code:
    Optimized Query: Mech Final Equip 3 Mth
    SELECT 
       Format$(T1.[Up Date/Time],'yyyy mm') AS DownMonth, 
       T1.[Final Equipment] AS DownEquip, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                      AND ([Forms]![H36 Date Selector]![End Date 1]),
               T1.Down,
               0)) AS DownSum3Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                      AND ([Forms]![H36 Date Selector]![End Date 1]),
               1,
               0)) AS DownCount3Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 2]) 
                      AND ([Forms]![H36 Date Selector]![End Date 2]),
               T1.Down,
               0)) AS DownSum6Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 2]) 
                      AND ([Forms]![H36 Date Selector]![End Date 2]),
               1,
               0)) AS DownCount6Mos, 
       Sum(T1.Down) AS DownSumHist, 
       Count(T1.Down) AS DownCountHist
    FROM
        [416 Data] AS T1
    WHERE (
           ((T1.[Final Equipment]) Is Not Null) 
       AND ((T1.[Status])="brkdwn") 
       AND ((T1.[Reason])="mech"))
          )
    GROUP BY 
       Format$(T1.[Up Date/Time],'yyyy mm'), 
       T1.[Final Equipment];
    You will notice that for 3-month and 6-month counts I am summing either a 1 or 0, rather than counting the records.

    The following query should get you one record per equipment, with columns for the 3-month, 6-month and historical filled out properly:
    Code:
    SELECT 
       T1.[Final Equipment] AS DownEquip, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                      AND ([Forms]![H36 Date Selector]![End Date 1]),
               T1.Down,
               0)) AS DownSum3Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 1]) 
                      AND ([Forms]![H36 Date Selector]![End Date 1]),
               1,
               0)) AS DownCount3Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 2]) 
                      AND ([Forms]![H36 Date Selector]![End Date 2]),
               T1.Down,
               0)) AS DownSum6Mos, 
       Sum(IIF(T1.[Up Date/Time]) 
                  BETWEEN ([Forms]![H36 Date Selector]![Start Date 2]) 
                      AND ([Forms]![H36 Date Selector]![End Date 2]),
               1,
               0)) AS DownCount6Mos, 
       Sum(T1.Down) AS DownSumHist, 
       Count(T1.Down) AS DownCountHist
    FROM 
       [416 Data] AS T1
    WHERE (
           ((T1.[Final Equipment]) Is Not Null) 
       AND ((T1.[Status])="brkdwn") 
       AND ((T1.[Reason])="mech"))
          )
    GROUP BY 
       T1.[Final Equipment];
    Attached Thumbnails Attached Thumbnails Capture.PNG  

  6. #6
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    remove one of the parens after ="mech"))

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

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