473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I create an Average table

Hi,
I am trying to create a function to receive a table with a changing number of columns (for each time the function is called).
The first column is always time, in the format "dd/mm/yyyy hh:nn:ss". Every ten seconds there is a new row with many columns of numerical data.
The user writes a number of minutes in a text box, for instance 10,
and the function returns a table containing all the original columns but the values would be averaged by ten minutes (less rows of course)

Thanks in advance,

Izhar
Jul 29 '10 #1
15 2436
ADezii
8,834 Expert 8TB
@izharmel
Kindly post some concrete data as well as the results that you would like to see after averaging. What exactly do you mean by Averaging by 10 Minutes?
Jul 29 '10 #2
OK, the original table would look like:

15/06/2010 12:31:00 1970 151.7 2360 116.8
15/06/2010 12:31:10 2000 163.4 2370 127.5
15/06/2010 12:31:20 1990 142.8 2360 112.7
15/06/2010 12:31:30 1990 142.8 2360 109.4
15/06/2010 12:31:40 1960 144.3 2360 110.7
15/06/2010 12:31:50 1950 157.2 2360 121.5
15/06/2010 12:32:00 1940 155.8 2360 122
.
.
.

and the the result table would look like that:

15/06/2010 12:31:00 1990 150.7 2340 114.2
15/06/2010 12:41:10 2020 167.4 2390 187.9
15/06/2010 12:51:20 1700 145.8 2330 165.4
.
.
.

where the values would be averages of the original list, upon periods of ten minutes.

What I've done so far is try to create a record set of the original data, and each time take ten minutes worth of data (using DateAdd) into another rs. then use AVG (SQL) but I don't know how to properly write this, or how to run a query without knowing the name of the column.

Thanks in advance
Izhar
Jul 29 '10 #3
ADezii
8,834 Expert 8TB
@izharmel
  1. I've created a Generic Code Template for you in the form of a Public Function. Simply pass to the Function the Name of a valid Table, Query, or SQL Statement. The Function will then calculate the Average of every Column in the Data Source 'except' the first Column. The Results will be displayed in the Debug Window as each Field Name (except the 1st), along with the Averages for those Fields. I've included basic Error Handling, but for the sake of brevity and simplicity, I'll leave the Validating and Formatting issues for you to resolve such as: making sure all Field Values are Numeric in nature, handling of NULLs, Formatting Averages to the proper number of decimal places, etc. The code is easily adaptable, and hopefully should point you in the right direction. Good Luck.
  2. Code Example:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcAveragesInTableValues(strTableName As String)
    2. On Error GoTo Err_fCalcAveragesInTableValues
    3. Dim MyDB As DAO.Database
    4. Dim rst As DAO.Recordset
    5. Dim lngNumOfRecs As Long
    6. Dim intFldCtr
    7. Dim lngRunningTotal
    8. Dim strFldName As String
    9.  
    10. If strTableName = "" Then Exit Function
    11.  
    12. Set MyDB = CurrentDb
    13. Set rst = MyDB.OpenRecordset(strTableName, dbOpenSnapshot)
    14.  
    15. rst.MoveLast: rst.MoveFirst
    16.  
    17. lngNumOfRecs = rst.RecordCount
    18. If lngNumOfRecs = 0 Then Exit Function
    19.  
    20. With rst
    21.   'Iterate all the Fields 'except' the 1st Field
    22.   For intFldCtr = 1 To .Fields.Count - 1
    23.     strFldName = .Fields(intFldCtr).Name
    24.       Do While Not .EOF
    25.         lngRunningTotal = lngRunningTotal + .Fields(intFldCtr).Value
    26.           .MoveNext
    27.       Loop
    28.        Debug.Print strFldName & " ==> " & lngRunningTotal / lngNumOfRecs
    29.         lngRunningTotal = 0       'Must Reset after each Iteration
    30.         .MoveFirst
    31.   Next
    32. End With
    33.  
    34. rst.Close
    35. Set rst = Nothing
    36.  
    37. Exit_fCalcAveragesInTableValues:
    38.   Exit Function
    39.  
    40. Err_fCalcAveragesInTableValues:
    41.   If Err.Number = 3078 Then
    42.     MsgBox "The Table [" & strTableName & "] does not exist in the Database", _
    43.             vbCritical, "Invalid Reference"
    44.   Else
    45.     MsgBox Err.Description, vbExclamation, "Error in fCalcAveragesInTableValues()"
    46.   End If
    47.     Resume Exit_fCalcAveragesInTableValues
    48. End Function
  3. Sample Call to Function ignoring any Return Value:
    Expand|Select|Wrap|Line Numbers
    1. Call fCalcAveragesInTableValues("Table1")
  4. Sample results based on Table1 containing 6 Fields, all LONG INTEGERS except the First Field:
    Expand|Select|Wrap|Line Numbers
    1. Field2 ==> 400
    2. Field3 ==> 8
    3. Field4 ==> 3000
    4. Field5 ==> 21
    5. Field6 ==> 33.3333333333333
Jul 29 '10 #4
Thank you very much,
I'll integrate it into what I already have and I'll let you know how it goes.
Aug 1 '10 #5
Hi,
I'm trying to combine your function into what I already have.
So far I have a record set with all the information I want.
I run a do while loop on it, to to take a 10 minute segment each time (for example) and pass it on to your function.

Expand|Select|Wrap|Line Numbers
  1. Do While Not rs.EOF
  2.         rs.MoveNext
  3.         'add num of minutes requested to the current time
  4.         time1 = DateAdd("n", AvNum, rs!Time)  
  5.         SQLrs = "SELECT * FROM rs WHERE rs!Time < time1"            
  6.         'take a segment of data to a new rs
  7.         Set rsTemp = db.OpenRecordset(SQLrs, dbOpenSnapshot)
  8.  
the SQLrs part is wrong...
How do I take a part of a record set into a new one?

Thanks in advance,
Izhar
Aug 2 '10 #6
ADezii
8,834 Expert 8TB
@izharmel
You included a Pointer to the Recordset (rs) in the SQL Statement, when you should have a Table or Tables listed instead. Also, point to the Field Name within the Table (assuming Table1):
Expand|Select|Wrap|Line Numbers
  1. SQLrs = "SELECT * FROM Table1 WHERE Table1.[Time] < #" & time1 & "#;"
P.S. - I created a Code demo for you, based on the Northwind Sample Database, that adds a sequential number of Months (1 to 11) to a Base Date (1/1/1963), while creating Recordsets in which Birth Dates are > the derived Dates.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim strSQL As String
  4. Dim intMonthCtr As Integer
  5.  
  6. strSQL = "SELECT * FROM Employees WHERE [BirthDate] > #"
  7.  
  8. Debug.Print "Counter", "Base Date", "Date", "Birth Date"
  9.  
  10. Set MyDB = CurrentDb
  11. Set rst = MyDB.OpenRecordset("Employees", dbOpenForwardOnly)
  12.  
  13. For intMonthCtr = 1 To 11
  14.  Set rst = MyDB.OpenRecordset(strSQL & DateAdd("m", intMonthCtr, #1/1/1963#) & _
  15.                               "#;", dbOpenForwardOnly)
  16.   With rst
  17.     Do While Not .EOF
  18.       Debug.Print intMonthCtr, "1/1/1963", DateAdd("m", intMonthCtr, #1/1/1963#), ![BirthDate]
  19.         .MoveNext
  20.     Loop
  21.       Debug.Print "---------------------------------------------------------"
  22.   End With
  23. Next
  24.  
  25. rst.Close
  26. Set rst = Nothing
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Counter       Base Date     Date          Birth Date
  2.  1            1/1/1963      2/1/1963      12/8/1968 
  3.  1            1/1/1963      2/1/1963      8/30/1963 
  4.  1            1/1/1963      2/1/1963      7/2/1963 
  5.  1            1/1/1963      2/1/1963      7/2/1969 
  6. ---------------------------------------------------------
  7.  2            1/1/1963      3/1/1963      12/8/1968 
  8.  2            1/1/1963      3/1/1963      8/30/1963 
  9.  2            1/1/1963      3/1/1963      7/2/1963 
  10.  2            1/1/1963      3/1/1963      7/2/1969 
  11. ---------------------------------------------------------
  12.  3            1/1/1963      4/1/1963      12/8/1968 
  13.  3            1/1/1963      4/1/1963      8/30/1963 
  14.  3            1/1/1963      4/1/1963      7/2/1963 
  15.  3            1/1/1963      4/1/1963      7/2/1969 
  16. ---------------------------------------------------------
  17.  4            1/1/1963      5/1/1963      12/8/1968 
  18.  4            1/1/1963      5/1/1963      8/30/1963 
  19.  4            1/1/1963      5/1/1963      7/2/1963 
  20.  4            1/1/1963      5/1/1963      7/2/1969 
  21. ---------------------------------------------------------
  22.  5            1/1/1963      6/1/1963      12/8/1968 
  23.  5            1/1/1963      6/1/1963      8/30/1963 
  24.  5            1/1/1963      6/1/1963      7/2/1963 
  25.  5            1/1/1963      6/1/1963      7/2/1969 
  26. ---------------------------------------------------------
  27.  6            1/1/1963      7/1/1963      12/8/1968 
  28.  6            1/1/1963      7/1/1963      8/30/1963 
  29.  6            1/1/1963      7/1/1963      7/2/1963 
  30.  6            1/1/1963      7/1/1963      7/2/1969 
  31. ---------------------------------------------------------
  32.  7            1/1/1963      8/1/1963      12/8/1968 
  33.  7            1/1/1963      8/1/1963      8/30/1963 
  34.  7            1/1/1963      8/1/1963      7/2/1969 
  35. ---------------------------------------------------------
  36.  8            1/1/1963      9/1/1963      12/8/1968 
  37.  8            1/1/1963      9/1/1963      7/2/1969 
  38. ---------------------------------------------------------
  39.  9            1/1/1963      10/1/1963     12/8/1968 
  40.  9            1/1/1963      10/1/1963     7/2/1969 
  41. ---------------------------------------------------------
  42.  10           1/1/1963      11/1/1963     12/8/1968 
  43.  10           1/1/1963      11/1/1963     7/2/1969 
  44. ---------------------------------------------------------
  45.  11           1/1/1963      12/1/1963     12/8/1968 
  46.  11           1/1/1963      12/1/1963     7/2/1969 
---------------------------------------------------------
Aug 2 '10 #7
Thank you
it works great
Aug 5 '10 #8
Hi,
I thought I had the function working...
It actually works fine for some months but when I tried averaging July it doesn't take any data from rs to rsTemp.
It's as if the [Time] column doesn't fulfill the terms for the SELECT Query.

Expand|Select|Wrap|Line Numbers
  1. Function Av_Calc(AvNum As Integer, TableNme As String)
  2.     Dim db As DAO.Database
  3.     Dim rs As DAO.Recordset
  4.     Dim rsTemp As DAO.Recordset
  5.     Dim SQLrs As String
  6.     Dim ShowField As String
  7.     Dim FromTable As String
  8.     Dim LastTime As Date
  9.     Dim TopTime As Date
  10.     Dim BottomTime As Date
  11.     Dim RowsNum As Integer
  12.  
  13.     Set db = CurrentDb
  14.     DoCmd.SetWarnings False
  15.     DoCmd.RunSQL "DROP TABLE T_Average" 'delete existing T_Average Table
  16.     'create a new table with the same columns as T_Export
  17.     DoCmd.TransferDatabase acExport, "Microsoft Access",_
  18.  db.Name, acTable, "T_Export", "T_Average", True
  19.  
  20.     Set rs = db.OpenRecordset(TableNme, dbOpenSnapshot)
  21.     rs.MoveLast
  22.     LastTime = rs!Time  'save the time of the last row for the stopping condition
  23.     rs.MoveFirst
  24.  
  25.     'stopping condition for a non round last loop
  26.     Do While Not rs!Time > DateAdd("n", -AvNum, LastTime)  
  27.         'add num of minutes requested to the current time
  28.         TopTime = DateAdd("n", AvNum, rs!Time) 
  29.         If TopTime > LastTime Then TopTime = LastTime
  30.         BottomTime = rs!Time
  31.         SQLrs = "SELECT * FROM " & TableNme & " WHERE " & TableNme & ".[Time] < #" & TopTime & "#"
  32.         SQLrs = SQLrs & " AND " & TableNme & ".[Time] >= #" & BottomTime & "#"
  33.         Set rsTemp = db.OpenRecordset(SQLrs, dbOpenDynaset) 'takes a segment of data to a new rs"
  34.         If rsTemp.EOF Then Exit Function
  35.         RowsNum = 0
  36.         Do While rs!Time < TopTime
  37.             RowsNum = RowsNum + 1
  38.             rs.MoveNext
  39.         Loop
  40.  
  41.         DoCmd.RunSQL "INSERT INTO T_Average([Time]) VALUES(#" & BottomTime & "#)"
  42.         With rsTemp
  43.             'Iterate all the Fields 'except' the 1st Field
  44.             For intFldCtr = 1 To .Fields.Count - 1
  45.                 Do While Not .EOF
  46.                     lngRunningTotal = lngRunningTotal + .Fields(intFldCtr).Value
  47.                     .MoveNext
  48.                 Loop
  49.                 SQLrs = "UPDATE T_Average SET [" & rsTemp.Fields(intFldCtr).Name & "] = "
  50.                 SQLrs = SQLrs & "" & lngRunningTotal / RowsNum & " WHERE "
  51.                 SQLrs = SQLrs & "[Time]=#" & BottomTime & "#"
  52.                 DoCmd.RunSQL SQLrs
  53.                 lngRunningTotal = 0       'Must Reset after each Iteration
  54.                 .MoveFirst
  55.             Next
  56.         End With
  57.         rsTemp.Close
  58.         Set rsTemp = Nothing
  59.     Loop
  60.  
  61.     rs.Close
  62.     db.Close
  63.     Set rs = Nothing
  64.     Set db = Nothing
  65.     DoCmd.SetWarnings True
  66. End Function
  67.  
an example of an input table would be:
Time Data1 Data2
01/07/2010 04:28:00 293 1059
01/07/2010 04:28:20 185 1040
01/07/2010 04:28:40 57 375
01/07/2010 04:28:50 49 172
01/07/2010 04:29:00 32 85
01/07/2010 04:29:20 16 0
01/07/2010 04:29:40 14 0
01/07/2010 04:30:00 112 0
01/07/2010 04:30:20 129 0
01/07/2010 04:30:40 118 783
01/07/2010 04:31:00 89 189
01/07/2010 04:31:20 61 113
01/07/2010 04:31:30 273 118
01/07/2010 04:31:40 300 150
01/07/2010 04:32:00 364 908
01/07/2010 04:32:20 55 260
01/07/2010 04:32:30 27 59
01/07/2010 04:32:40 85 47
01/07/2010 04:32:50 39 49
01/07/2010 04:33:00 68 68
01/07/2010 04:33:20 81 82
01/07/2010 04:33:40 137 207
01/07/2010 04:33:50 125 422
01/07/2010 04:34:00 105 238
01/07/2010 04:34:10 134 194
01/07/2010 04:34:20 100 300
01/07/2010 04:34:40 101 250
01/07/2010 04:34:50 40 194
01/07/2010 04:35:00 23 59
01/07/2010 04:35:10 24 44

As I said, it works fine for some months but with this case it gives a blank table (because of the If rsTemp.EOF condition)

Can any one help?

Thanks in advance,

Izhar
Aug 5 '10 #9
ADezii
8,834 Expert 8TB
Izhar, it makes no sense to me that thee Logic will work for some Months but not for July. If rstTemp.EOF evaluates to True, or more specifically if (rstTemp.BOF And rstTemp.EOF) evaluates to True, then there are simply no Records which meet the 10 Minute Range Criteria. Could this simply be the case?
Aug 5 '10 #10
I wish it were.
Since even when the function works, it's very slow (approx 10 minutes) I decided to try an approach it from an SQL point of view.
so far I have :
"SELECT [Time], AVG(other fields), INTO 'new Table' FROM 'old table' GROUP BY (time/60/AvNum)"
I think the average part is correct but displaying the time doesn't work well...
Aug 8 '10 #11
I'm kinda stuck...
Expand|Select|Wrap|Line Numbers
  1. Function Av_Calc(AvNum As Integer, TableNme As String)
  2.     Dim db As DAO.Database
  3.     Dim rs As DAO.Recordset
  4.     Dim SQLrs As String
  5.     Dim ShowField As String
  6.     Dim Diff As Long
  7.  
  8.     Set db = CurrentDb
  9.     DoCmd.SetWarnings False
  10.     DoCmd.RunSQL "DROP TABLE T_Average" 'delete existing T_Average Table
  11.     'create a new table with the same columns as T_Export
  12.     DoCmd.TransferDatabase acExport, "Microsoft Access", db.Name, acTable, "T_Export", "T_Average", True
  13.  
  14.     Set rs = db.OpenRecordset(TableNme, dbOpenSnapshot)
  15.     ' create a string containing the names of the fields in the table
  16.     For i = 1 To rs.Fields.Count - 1
  17.         ShowField = ShowField & ", AVG(" & rs.Fields(i).Name & ") "
  18.         ShowField = ShowField & "AS 'AVG(" & rs.Fields(i).Name & ")'"
  19.     Next
  20.  
  21.     Diff = DateDiff("s", "01/01/2010 00:00:00", rs![Time]) / 60 / AvNum
  22.     SQLrs = "SELECT " & Diff & " AS 'Time'" & ShowField & ""
  23.     SQLrs = SQLrs & " INTO T_Average FROM T_Export GROUP BY " & Diff & ""
  24.     DoCmd.RunSQL SQLrs
  25.  
  26.     rs.Close
  27.     db.Close
  28.     Set rs = Nothing
  29.     Set db = Nothing
  30.     DoCmd.SetWarnings True
  31. End Function
  32.  
The function doesn't choose the AvNum segments properly.
So far it shows only one row, with the date part as a long number
Aug 8 '10 #12
ADezii
8,834 Expert 8TB
I feel as though you are hitting a Dead End as far as the 10-Minute Segment creations go, thus a new approach:
  1. Calculate the Overall Starting Date/Time
  2. Calculate the Overall Ending Date/Time
  3. Calculate the difference, in Minutes, between the Overall Start and End Date/Times
  4. Calculate the Number of 10 Minute Segments in the difference quoted above
  5. Loop thru all the 10 Minute Segments
  6. Generate the appropriate SQL for each Interval
  7. Loop as long as the End of a Calculated Segment is not > the Absolute Ending Date/Time
  8. Figure out what you will do for the Overlapping Intervals
  9. Demo Code below and Results achieved via Dummy Data
    Expand|Select|Wrap|Line Numbers
    1. Dim MyDB As DAO.Database
    2. Dim rst As DAO.Recordset
    3. Dim dteStartTime As Date
    4. Dim dteEndTime As Date
    5. Dim intMinsDiff As Integer
    6. Dim intNumOf10MinBlocks As Integer
    7. Dim intCtr As Integer
    8.  
    9. Set MyDB = CurrentDb()
    10. Set rst = MyDB.OpenRecordset("SELECT * FROM tblTimes ORDER BY [TheTime];", dbOpenSnapshot)
    11.  
    12. rst.MoveFirst: dteStartTime = rst![TheTime]
    13. rst.MoveLast: dteEndTime = rst![TheTime]
    14.  
    15. intMinsDiff = DateDiff("n", dteStartTime, dteEndTime)
    16. intNumOf10MinBlocks = intMinsDiff \ 10
    17.  
    18. For intCtr = 0 To intNumOf10MinBlocks
    19.   If DateDiff("n", DateAdd("n", intCtr * 10, dteStartTime), dteEndTime) >= 10 Then
    20.     Debug.Print "SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #" & DateAdd("n", intCtr * 10, dteStartTime) & _
    21.                 "# AND #" & DateAdd("n", 10, DateAdd("n", intCtr * 10, dteStartTime)) & "#;"
    22.       'Average processing for each Segment here
    23.   End If
    24. Next
    Expand|Select|Wrap|Line Numbers
    1. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:00:00 AM# AND #8/8/2010 8:10:00 AM#;
    2. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:10:00 AM# AND #8/8/2010 8:20:00 AM#;
    3. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:20:00 AM# AND #8/8/2010 8:30:00 AM#;
    4. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:30:00 AM# AND #8/8/2010 8:40:00 AM#;
    5. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:40:00 AM# AND #8/8/2010 8:50:00 AM#;
    6. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:50:00 AM# AND #8/8/2010 9:00:00 AM#;
    7. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:00:00 AM# AND #8/8/2010 9:10:00 AM#;
    8. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:10:00 AM# AND #8/8/2010 9:20:00 AM#;
    9. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:20:00 AM# AND #8/8/2010 9:30:00 AM#;
    10. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:30:00 AM# AND #8/8/2010 9:40:00 AM#;
    11. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:40:00 AM# AND #8/8/2010 9:50:00 AM#;
    12. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:50:00 AM# AND #8/8/2010 10:00:00 AM#;
    13. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 10:00:00 AM# AND #8/8/2010 10:10:00 AM#;
    14. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 10:10:00 AM# AND #8/8/2010 10:20:00 AM#;
    15. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 10:20:00 AM# AND #8/8/2010 10:30:00 AM#;
    16. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 10:30:00 AM# AND #8/8/2010 10:40:00 AM#;
    17. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 10:40:00 AM# AND #8/8/2010 10:50:00 AM#;
    18. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 10:50:00 AM# AND #8/8/2010 11:00:00 AM#;
    19. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 11:00:00 AM# AND #8/8/2010 11:10:00 AM#;
    20. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 11:10:00 AM# AND #8/8/2010 11:20:00 AM#;
    21. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 11:20:00 AM# AND #8/8/2010 11:30:00 AM#;
    22. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 11:30:00 AM# AND #8/8/2010 11:40:00 AM#;
    23. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 11:40:00 AM# AND #8/8/2010 11:50:00 AM#;
    24. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 11:50:00 AM# AND #8/8/2010 12:00:00 PM#;
    25. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 12:00:00 PM# AND #8/8/2010 12:10:00 PM#;
    26. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 12:10:00 PM# AND #8/8/2010 12:20:00 PM#;
    27. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 12:20:00 PM# AND #8/8/2010 12:30:00 PM#;
    28. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 12:30:00 PM# AND #8/8/2010 12:40:00 PM#;
    29. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 12:40:00 PM# AND #8/8/2010 12:50:00 PM#;
    30. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 12:50:00 PM# AND #8/8/2010 1:00:00 PM#;
    31. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 1:00:00 PM# AND #8/8/2010 1:10:00 PM#;
    32. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 1:10:00 PM# AND #8/8/2010 1:20:00 PM#;
    33. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 1:20:00 PM# AND #8/8/2010 1:30:00 PM#;
    34. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 1:30:00 PM# AND #8/8/2010 1:40:00 PM#;
    35. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 1:40:00 PM# AND #8/8/2010 1:50:00 PM#;
    36. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 1:50:00 PM# AND #8/8/2010 2:00:00 PM#;
    37. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 2:00:00 PM# AND #8/8/2010 2:10:00 PM#;
    38. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 2:10:00 PM# AND #8/8/2010 2:20:00 PM#;
    39. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 2:20:00 PM# AND #8/8/2010 2:30:00 PM#;
    40. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 2:30:00 PM# AND #8/8/2010 2:40:00 PM#;
    41. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 2:40:00 PM# AND #8/8/2010 2:50:00 PM#;
    42. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 2:50:00 PM# AND #8/8/2010 3:00:00 PM#;
    43. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 3:00:00 PM# AND #8/8/2010 3:10:00 PM#;
    44. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 3:10:00 PM# AND #8/8/2010 3:20:00 PM#;
    45. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 3:20:00 PM# AND #8/8/2010 3:30:00 PM#;
    46. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 3:30:00 PM# AND #8/8/2010 3:40:00 PM#;
    47. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 3:40:00 PM# AND #8/8/2010 3:50:00 PM#;
    48. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 3:50:00 PM# AND #8/8/2010 4:00:00 PM#;
    49. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 4:00:00 PM# AND #8/8/2010 4:10:00 PM#;
    50. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 4:10:00 PM# AND #8/8/2010 4:20:00 PM#;
    51. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 4:20:00 PM# AND #8/8/2010 4:30:00 PM#;
    52. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 4:30:00 PM# AND #8/8/2010 4:40:00 PM#;
    53. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 4:40:00 PM# AND #8/8/2010 4:50:00 PM#;
    54. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 4:50:00 PM# AND #8/8/2010 5:00:00 PM#;
    55. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 5:00:00 PM# AND #8/8/2010 5:10:00 PM#;
    56. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 5:10:00 PM# AND #8/8/2010 5:20:00 PM#;
    57. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 5:20:00 PM# AND #8/8/2010 5:30:00 PM#;
    58. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 5:30:00 PM# AND #8/8/2010 5:40:00 PM#;
    59. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 5:40:00 PM# AND #8/8/2010 5:50:00 PM#;
    60. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 5:50:00 PM# AND #8/8/2010 6:00:00 PM#;
    61. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 6:00:00 PM# AND #8/8/2010 6:10:00 PM#;
    62. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 6:10:00 PM# AND #8/8/2010 6:20:00 PM#;
    63. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 6:20:00 PM# AND #8/8/2010 6:30:00 PM#;
    64. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 6:30:00 PM# AND #8/8/2010 6:40:00 PM#;
    65. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 6:40:00 PM# AND #8/8/2010 6:50:00 PM#;
    66. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 6:50:00 PM# AND #8/8/2010 7:00:00 PM#;
    67. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 7:00:00 PM# AND #8/8/2010 7:10:00 PM#;
    68. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 7:10:00 PM# AND #8/8/2010 7:20:00 PM#;
    69. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 7:20:00 PM# AND #8/8/2010 7:30:00 PM#;
    70. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 7:30:00 PM# AND #8/8/2010 7:40:00 PM#;
    71. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 7:40:00 PM# AND #8/8/2010 7:50:00 PM#;
    72. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 7:50:00 PM# AND #8/8/2010 8:00:00 PM#;
    73. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:00:00 PM# AND #8/8/2010 8:10:00 PM#;
    74. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:10:00 PM# AND #8/8/2010 8:20:00 PM#;
    75. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:20:00 PM# AND #8/8/2010 8:30:00 PM#;
    76. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:30:00 PM# AND #8/8/2010 8:40:00 PM#;
    77. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:40:00 PM# AND #8/8/2010 8:50:00 PM#;
    78. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 8:50:00 PM# AND #8/8/2010 9:00:00 PM#;
    79. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:00:00 PM# AND #8/8/2010 9:10:00 PM#;
    80. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:10:00 PM# AND #8/8/2010 9:20:00 PM#;
    81. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:20:00 PM# AND #8/8/2010 9:30:00 PM#;
    82. SELECT * FROM tblTimes WHERE [TheTime] BETWEEN #8/8/2010 9:30:00 PM# AND #8/8/2010 9:40:00 PM#;
Aug 8 '10 #13
Thank you for the huge help.
I'll also try to change my approach and tell you how it goes.
Aug 9 '10 #14
Delerna
1,134 Expert 1GB
SQL is the way to go if you want to improve the performance
Does this help?
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO newTable
  2. SELECT min([Time]),avg(otherfields)
  3. FROM
  4. (  SELECT year([Time]) as yr,
  5.           month([Time]) as mth,
  6.           day([Time]) as dy,
  7.           hour([Time]) as hr,
  8.           int(minute([Time])/varMins)*varMins as mn,  
  9.           [Time],
  10.           {other fields)
  11.     FROM [old table]
  12. )a
  13. GROUP BY yr,mth,dy,hr,mn
  14.  
where varMins
is the entered number of minutes
Aug 9 '10 #15
hi,
I just fixed the problem (after days of work)

here's the code:
Expand|Select|Wrap|Line Numbers
  1. Function Av_Calc(AvNum As Integer, TableNme As String)
  2.     Dim db As DAO.Database
  3.     Dim rs As DAO.Recordset
  4.     Dim SQLrs As String
  5.     Dim ShowField As String
  6.     Dim Diff As Long
  7.  
  8.     Set db = CurrentDb
  9.     DoCmd.SetWarnings False
  10.     DoCmd.RunSQL "DROP TABLE T_Average" 'delete existing T_Average Table
  11.     'create a new table with the same columns as T_Export
  12.     DoCmd.TransferDatabase acExport, "Microsoft Access", db.Name, acTable, "T_Export", "T_Average", True
  13.  
  14.     Set rs = db.OpenRecordset(TableNme, dbOpenSnapshot)
  15.     ' create a string containing the names of the fields in the table
  16.     For i = 1 To rs.Fields.Count - 1
  17.         ShowField = ShowField & ", AVG(" & rs.Fields(i).Name & ") "
  18.         ShowField = ShowField & "AS 'AVG(" & rs.Fields(i).Name & ")'"
  19.     Next
  20.  
  21.     SQLrs = "SELECT Min([Time]) "
  22.     SQLrs = SQLrs & "AS 'Time'" & ShowField & " INTO T_Average FROM T_Export GROUP BY"
  23.     SQLrs = SQLrs & " Int(DateDiff('s', '01/01/2010 00:00:00', [Time])/60/" & AvNum & ")"
  24.     DoCmd.RunSQL SQLrs
  25.  
  26.     rs.Close
  27.     db.Close
  28.     Set rs = Nothing
  29.     Set db = Nothing
  30.     DoCmd.SetWarnings True
  31. End Function
  32.  
Thank you all very much for your help
Aug 9 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mh | last post by:
Hello, I'm trying to create a table from a PHP script. >From what I read in my book about PHP and MySQL I should do something like that: $sql_query="CREATE TABLE '$num' ( 'variable1' ...
6
by: wk | last post by:
i have a <table></table> ...and the have a string with the html that will create the table rows, columsn etc....perhaps <tr><td>a row</td></tr>. this string comes dynamically from a source and...
1
by: poohnie08 | last post by:
i have a excel spreadsheet showing staff name, date,work hour, ot hour, slot1, slot2, slot3, slot4 and others). The "()" will keep repeating from day 1 until end of month. eg in excel spreadsheet,...
2
by: Alex Boborikin | last post by:
I create a table in MS access: CREATE TABLE TABLE1 (_ID COUNTER PRIMARY KEY, SESSION_ID INTEGER REFERENCES SESSIONS (SESSION_ID), FIELD1 INTEGER, FIELD2 INTEGER, ); session_id is linked to...
4
by: Ying Lu | last post by:
Hello, I have a table named "USER" under MySQL database. When I am trying to move tables from MySQL to PostgreSQL, I found that I could not create a table namely "USER". I guess "USER" is a key...
7
by: Genus Neduba | last post by:
Hi, is it actually possible to create a table that has a UDT as a column datatype? e.g.: CREATE TYPE addressType AS ( street INTEGER, zip VARCHAR(30)) MODE DB2SQL
2
by: lakuma | last post by:
Hi, I have a table called A (say) with columns called name, place, animal and thing. I would want to write an on insert trigger on this table, which would create a table with the name of the...
3
by: Rahul B | last post by:
Hi, I have a user UCLDEV1 which is a part of staff and a group(db2schemagrp1) to which i have not given any permissions. The authorizations of that user are shown as db2 =get authorizations...
1
by: noorulahilah | last post by:
How can i create the table through coding in access? And how can i connect with vb. Suppose "db" is database variable. "stu1" is already existing table name. Now i want to create table "stu2"...
1
by: teneesh | last post by:
I'm attempting something new, in trying to create a table in VBA coming from excel. I saw an example using ADOB but I don't want to use that, just some plain script. I've set up this example to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.