Page 1 of 4 1234 LastLast
Results 1 to 15 of 59
  1. #1
    dr4ke1990 is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    52

    Calculating Multiple Differences

    Good afternoon all,




    I would like to create a query to calculate the differences in Servers/Disks disk space between a date specified by a form. I have successfully built the query to only show data between two dates (below) but the other requirement is still outstanding ... I've carried this sentence on below the code window so it doesn't get messy.

    Code:
     SELECT *From DiskSpaceStats
    Where ((DiskSpaceStats.Date_Of_Report) Between (Forms!FrontPage!SelectStart) And (Forms!FrontPage!SelectEnd))

    So the below table is an example of what is in my Database Table. What I would like to have is a calculation to tell me the difference between all dates; in the case of Server1 & Disk ID A I would need the difference of Disk_Space_Used between 14/10/2013 & 15/10/2013 (5), 15/10/2013 & 16/10/2013 (5) and 16/10/2013 & 17/10/2013 (5).

    The important thing to note is that as time continues more and more dates will be added to the Table, and subsequently the query, over time so this needs to be automated and not something I have to update continuously.

    I know I could calculate the difference between the start date (15/10/2013) and end date (16/10/2013) using MIN and MAX, but then I miss out all the data in between which I don't want to do.


    ID Computer Name Disk ID Disk Size MB Percentage_Used Disk_Space_Used Date_Of_Report
    1 Server1 A 50 98 35 14/10/2013
    2 Server1 B 88 35 15 14/10/2013
    3 Server2 A 120 24 46 14/10/2013
    4 Server1 A 50 98 40 15/10/2013
    5 Server1 B 88 40 20 15/10/2013
    6 Server2 A 120 30 50 15/10/2013
    7 Server1 A 50 99 45 16/10/2013
    8 Server1 B 88 45 25 16/10/2013
    9 Server2 A 120 35 60 16/10/2013
    10 Server1 A 50 100 50 17/10/2013
    11 Server1 B 88 50 30 17/10/2013
    12 Server2 A 120 40 65 17/10/2013

    Once I have achieved that I will want to add up all of these values and find out the average of each server/disk combination increase on a daily basis. If each 'difference' creates an additional column, I'm not sure how I'll achieve this so if that could be taken into account with any solutions that would be great!

    If I am approaching this in completely the wrong way, and over complicating it, please do let me know and I'm more than willing to change my strategy. The outcome I'm after is to add up all the differences and divide by the days taken into account (DATEDIFF) to get an average daily growth..

    Any help is very much appreciated in advance.....

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Will there be data for every day for every server (no gaps)? One approach is to do a query that calculates the previous date (or next date) for each record. Then join that query to the original table dataset by linking on the Server, Disk and the original date of report and the calculated date fields.

    Also review: http://allenbrowne.com/subquery-01.html#AnotherRecord
    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
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    So, you want the deltas for each day in the period.

    I would drive this testing by a temporary date table. I assume you can create such a table without help. Delete everything in the table and loop VBA to insert one record for each date combination. (This gives you the flexibility of later deciding you want to compare weekly or monthly as well.) The following also assumes that the DiskSize is not changable.
    Code:
    tblDates
       PrevDate
       NewDate
    
    tblUsage
       ReptID
       CompName
       DiskID
       DiskSize
       PctUsed
       Amtused
       ReptDate
    
    SELECT 
       TD.Date1 AS PrevDate, 
       TD.Date2 AS NewDate, 
       TU1.CompName, 
       TU1.DiskID, 
       TU1.DiskSize,
       TU1.PctUsed AS PrevPctUsed, 
       TU2.PctUsed AS NewPctUsed, 
       (TU2.PctUsed - TU1.PctUsed) AS ChgPctUsed,
       TU1.AmtUsed AS PrevAmtUsed, 
       TU2.PctUsed AS NewAmtUsed, 
       (TU2.AmtUsed - TU1.AmtUsed) AS ChgAmtUsed
    FROM 
       tblUsage AS TU2 
       INNER JOIN
           (tblUsage AS TU1
            INNER JOIN
            tblDates AS TD
            ON TD.PrevDate = TU1.ReptDate)
       ON (TD.NewDate = TU2.ReptDate) AND
          (TU1.CompName = TU2.CompName) AND
          (TU1.DiskID = TU2.DiskID);

  4. #4
    dr4ke1990 is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    52
    Thank you both. June - it isn't guaranteed there will be no gaps, I have to take into account server issues that prevent data gathering...

    Dal - sorry don't fully understand.but it's also not guaranteed that the disk size won't change. It's the nature of these servers that they will need to be increased over time...

    It seems the only way to do this is to create a query for each server and find a way to calculate this in each query and then join all together with a union or something... Or maybe it's just not possible?

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    If you want to calculate the difference between values in different records, that can be done with a subquery. Review http://allenbrowne.com/subquery-01.html#AnotherRecord

    My first suggestion could be constructed as an all-in-one nested query but it depended on no gaps in date sequence. Allen Browne's article shows a method using the TOP parameter and relies on unique ID and dates to properly order the records.
    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
    dr4ke1990 is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    52
    Thanks June - having read the article you recommended, I'm not sure this is going to be suitable for my needs; there is no guarantee that every server will have a record every day. Also, this gets difficult when you need it to add differences from multiple sources.

    I'm starting to wonder whether what I am trying to is best done in the method I'm currently trying to build.

    Any views as to whether this would be better done with 1 query per server, and then running queries to calculate the differences per date and then totalling and averaging that way. Seems a bit overkill, but I can't think of another way...

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Allen Browne's structure does not require a record every day, just that the date would not be repeated for each primary, in your case the server/drive.

    What do you mean by 'multiple sources'? Multiple tables? If data is all in one table, can be accomplished with one nested query. Allen's example looks ideal for your data.
    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.

  8. #8
    dr4ke1990 is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    52
    ah ok - in which I case I can guarantee that there won't be two records with the same date...

    I'll need to re-read the article as I clearly missed something fundamental. Would you be so kind to point me to the section that applies to my requirement?

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    The link puts it right in front of you - Get the value in another record.

    Consider:

    SELECT *, (SELECT TOP 1 [Space Used] FROM Table1 AS Dupe WHERE Dupe.[Server Name]=Table1.[Server Name] AND Dupe.[Disk Name]=Table1.[Disk Name] AND Dupe.DateRead<Table1.DateRead ORDER BY DateRead DESC, [Server Name], [Disk Name]) AS PriorRead FROM Table1;


    Spaces and special characters/punctuation (underscore is exception) should be avoided in names.
    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.

  10. #10
    dr4ke1990 is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    52
    Hi June - Thank you (again!).

    Firstly, as I understand it this is only going to return 2 values; the current and the one previous. If I want to compare the last 6 months data (taking every weeks difference into account and then finding the average growth) - does this mean I'd have to create hundreds of queries? The calculations I need to do are never going to be such that I only need to compare two dates; I'm currently achieving this through form entries and the MIN & MAX values.

    Secondly, with regards to spaces and special characters/punctuation; this is being addressed :-)

  11. #11
    dr4ke1990 is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    52
    Further update - I've had a bit of a play (using the code below) and limited to one server/disk ID so I can test my workings out. I have removed the server/disk names for obvious reasons. So this does give me what I want and I'll be able to average the growth of each server... My question now is, can I do this for all servers, and find out the average growth by server in just one query? Or will I need to now setup sub queries to drag to data out of this one to give me the averages?

    Code:
     SELECT (SELECT TOP 1 [Disk_Space_Used] FROM DiskSpaceStats AS Dupe WHERE Dupe.[Computer Name]=DiskSpaceStats.[Computer Name] AND Dupe.[Disk ID]=DiskSpaceStats.[Disk ID] AND Dupe.Date_Of_Report<DiskSpaceStats.Date_Of_Report ORDER BY Date_Of_Report DESC, [Computer Name], [Disk ID]) AS PriorRead, 
    DiskSpaceStats.[COMPUTER NAME], DiskSpaceStats.[DISK ID], DiskSpaceStats.[DISK SIZE MB], DiskSpaceStats.Percentage_Used, DiskSpaceStats.Disk_Space_Used, DiskSpaceStats.Date_Of_Report, 
    ((Disk_Space_Used)-(PriorRead)) As DifferenceUsed
    
    FROM DiskSpaceStats
    WHERE (((DiskSpaceStats.[COMPUTER NAME])=" Server1") AND ((DiskSpaceStats.[DISK ID])=" A"))
    ORDER BY DiskSpaceStats.Date_Of_Report DESC;
    PriorRead COMPUTER NAME DISK ID DISK SIZE MB Percentage_Used Disk_Space_Used Date_Of_Report DifferenceUsed
    22945.225882867 30,725 74.738780487805 22,963.49 25/10/2013 18.2644220110014
    22928.435234375 30,725 74.679335664336 22,945.23 24/10/2013 16.7906484919986
    22919.822264808 30,725 74.6246875 22,928.44 23/10/2013 8.6129695669988
    22906.842387153 30,725 74.596655052265 22,919.82 22/10/2013 12.9798776550015
    22909.159512195 30,725 74.554409722222 22,906.84 21/10/2013 -2.31712504200186
    22933.867979094 30,725 74.561951219512 22,909.16 18/10/2013 -24.7084668989992
    22933.408575175 30,725 74.642369337979 22,933.87 15/10/2013 0.459403919001488
    30,725 74.640874125874 22,933.41 14/10/2013

  12. #12
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Aircode, but it would look something like this -
    Code:
    SELECT 
       Dupe.PriorRead
       DS.[COMPUTER NAME], 
       DS.[DISK ID], 
       DS.[DISK SIZE MB], 
       DS.Percentage_Used, 
       DS.Disk_Space_Used, 
       DS.Date_Of_Report, 
       ((DS.Disk_Space_Used)-(Dupe.PriorRead)) As DifferenceUsed
    FROM 
       DiskSpaceStats AS DS,
       (SELECT Dupe.[Disk_Space_Used] AS PriorRead
        FROM DiskSpaceStats AS Dupe 
        WHERE Dupe.[Computer Name]=DS.[Computer Name] 
          AND Dupe.[Disk ID]=DS.[Disk ID] 
          AND Dupe.Date_Of_Report = 
              (SELECT MAX(D2.Date_Of_Report) 
               FROM DiskSpaceStats AS D2 
               WHERE D2.[Computer Name]=DS.[Computer Name] 
               AND D2.[Disk ID]=DS.[Disk ID] 
               AND D2.Date_Of_Report < DS.Date_Of_Report)) 
    ORDER BY DS.Date_Of_Report DESC;
    This is way too ugly with an unnecessary multilevel subselect - see post 14 for cleaner version.
    Last edited by Dal Jeanis; 10-29-2013 at 09:32 AM.

  13. #13
    dr4ke1990 is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    52
    Hi Dal - I tried copying and pasting this into my database to see the outcome, but it isn't working.

    I was going to ask this once I'd seen the results, to ensure it fits my needs, but could you give me a break down of the code you have written so I can tailor it and undertand it for future reference?

    Also, what is aircode?

  14. #14
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Aside from cleaning up your field names, this code should read almost exactly the way you need. I've standardized all your field names on underscores rather than (blegh) spaces, and capitalized only the first letter of each word in the field name. Personally, I'd shorten all those names further, kill all the underscores and switch to CamelCase, but it's your database.
    Code:
    SELECT 
       DS1.Date_Of_Report As CurrentRead, 
       DS2.Date_Of_Report As PriorRead,
       DS1.Computer_Name, 
       DS1.Disk_ID, 
       DS1.Disk_Size_MB As CurrentSize, 
       DS2.Disk_Size_MB As PriorSize, 
       (DS1.Disk_Size_MB - DS2.Disk_Size_MB) As ChgInSize, 
       DS1.Percentage_Used AS CurrentPct, 
       DS2.Percentage_Used As PriorPct, 
       (DS1.Percentage_Used - DS2.Percentage_Used) As ChgInPct, 
       DS1.Disk_Space_Used As CurrentUsed, 
       DS2.Disk_Space_Used As PriorUsed, 
       (DS1.Disk_Space_Used - DS2.Disk_Space_Used) As ChgInUsed 
    FROM 
       DiskSpaceStats AS DS1
       INNER JOIN 
       DiskSpaceStats AS DS2
       ON DS2.Computer_Name = DS1.Computer_Name 
       AND DS2.Disk_ID = DS1.Disk_ID 
    WHERE DS2.Date_Of_Report = 
         (SELECT MAX(DS3.Date_Of_Report) 
         FROM DiskSpaceStats AS DS3 
         WHERE DS1.Computer_Name = DS3.Computer_Name 
         AND DS1.Disk_ID = DS3.Disk_ID 
         AND DS1.Date_Of_Report > DS3.Date_Of_Report)
    ORDER BY 
       DS1.Date_Of_Report DESC,
       DS1.Computer_Name, 
       DS1.Disk_ID;

    Explanation of SQL:
    I've aliased your table two different ways (As DS1, As DS2) in order to join it to itself on Computer_Name and Disk_ID. DS1 represents the current record on any given date, DS2 represents the most recent record that existed prior to that date for the same computer and disk.

    The WHERE clause is the way we determine what the "most recent prior record" is. We do a subselect on a third alias (DS3) of the same table, to find the latest (MAX) Date_Of_Report in the table for that computer_Name and DISK_ID, that is less than the record on DS1. Whatever date that might be, is the date of the record that we use for DS2.

    If you wanted to do a rolling one-week comparison, you could modify the subselect to something like this:
    Code:
    WHERE DS2.Date_Of_Report = 
         (SELECT MAX(DS3.Date_Of_Report) 
         FROM DiskSpaceStats AS DS3 
         WHERE DS1.Computer_Name = DS3.Computer_Name 
         AND DS1.Disk_ID = DS3.Disk_ID 
         AND DS1.Date_Of_Report >= DateAdd("w",1,DS3.Date_Of_Report));
    You can adjust the DateAdd function's first and second parm to change the rolling lookback to any desired time period.

    PS Aircode means I typed it in without testing it. This is not aircode. If you change your field names to match the ones in this SQL, then the code will run as is, and produce something like the following against your test data.

    Click image for larger version. 

Name:	chgUse.PNG 
Views:	40 
Size:	20.7 KB 
ID:	14218

  15. #15
    dr4ke1990 is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    52
    Thanks Dal - I'm struggling a bit with the DS1, DS2 and DS3. I can't see where these have been defined; what is the difference between the 3 Alias table names?

Page 1 of 4 1234 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 09-27-2013, 09:44 AM
  2. Replies: 3
    Last Post: 04-24-2013, 10:44 AM
  3. Replies: 5
    Last Post: 11-27-2012, 10:24 PM
  4. Compare tables and tell me differences
    By cowboy in forum Programming
    Replies: 2
    Last Post: 08-11-2010, 08:32 AM
  5. INNER JOIN differences from SQL Server
    By naurispunk in forum Queries
    Replies: 0
    Last Post: 07-22-2010, 03:17 AM

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