473,322 Members | 1,496 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,322 software developers and data experts.

Create a column populated with numbers sequentially that look like 1, 1, 2, 2, 3, 3 ?

I'm wondernig if it's possible to do the following:

I'd like to average every 2 numeric values in a table as they appear
sequentially ordered by their time stamps. I was thinking the only way
to do this is to create another column in the table that is populated
with sequential numbers that increase by 1 every two values like so:

1
1
2
2
3
3

And then I can run the average function grouping by this new number
column.

Of course, I cannot figure out how to create this customized autonumber
column.

Any ideas?

Or any ideas for my main goal? How to average every two numeric values
ordered by timestamp?

Thank you!

May 12 '06 #1
3 2331
sh********@yahoo.com wrote:
I'm wondernig if it's possible to do the following:

I'd like to average every 2 numeric values in a table as they appear
sequentially ordered by their time stamps. I was thinking the only way
to do this is to create another column in the table that is populated
with sequential numbers that increase by 1 every two values like so:

1
1
2
2
3
3

And then I can run the average function grouping by this new number
column.

Of course, I cannot figure out how to create this customized autonumber
column.

Any ideas?

Or any ideas for my main goal? How to average every two numeric values
ordered by timestamp?

Thank you!


This routine is a demonstration of how you can get the sequence numbers
you desire. It will display the sequence counts for the first 10
records of a recordset. In your case the SQL might be something like
Dim strSQL As STring
strSQL = "Select * from table1 order by DateTimeStamp"
Set r = currentdb.openrecordset(strSQL,dbopensnapshot)

Create a new code module and insert this code there. If you
misunderstand anything, highlight the word and press F1 for help.

You will need to modify it to suit your needs. Change the table name to
any table you have in the app and run the code.

Sub AP()
Dim r As Recordset
Dim lngMod As Integer 'hold modulus
Dim lngCnt As Long 'do for 10 records
Dim lngPos As Long

'change the table name here to reflect you SQL string/query name.
'You should short the table by date/time stamp when opening the
'recordset.
Set r = CurrentDb.OpenRecordset("Table1", dbOpenSnapshot)
If r.RecordCount > 0 Then

Do While lngCnt < 10

lngCnt = lngCnt + 1 'only display 10 recs
lngPos = r.AbsolutePosition + 1 'start at #1...num recs
lngMod = lngPos Mod 2 'is this odd or even
If lngMod = 0 Then lngMod = 2 'go back 2 if even, 1 if odd

MsgBox "Record Count " & lngCnt & _
" Pos " & lngPos & _
" Mod " & lngMod & _
" AvgCnt " & ((lngPos - lngMod) / 2) + 1

'Note: avgcnt is the absolute position + 1 (start at 1, not 0)
'and subtract that by 1 or 2 to get the last even number.
'Divide the result of the last even number by 2 to get the
'highest count prior to this record and then increment by 1 to
'get the recs avgcnt number

r.MoveNext
If r.EOF Then Exit Do
Loop
end if

r.Close
Set r = Nothing
End Sub
May 12 '06 #2
sh********@yahoo.com wrote:
I'm wondernig if it's possible to do the following:

I'd like to average every 2 numeric values in a table as they appear
sequentially ordered by their time stamps. I was thinking the only way
to do this is to create another column in the table that is populated
with sequential numbers that increase by 1 every two values like so:

1
1
2
2
3
3

qryStratify:
SELECT X, ((SELECT Count(A.TimeStamp) FROM tblData AS A WHERE
A.TimeStamp < tblData.TimeStamp)+2)\2 AS Tier FROM tblData ORDER BY
TimeStamp;
And then I can run the average function grouping by this new number
column.
qryAvgX:
SELECT Tier, Avg(X) AS AvgX FROM qryStratify GROUP BY Tier;

Of course, I cannot figure out how to create this customized autonumber
column.

Any ideas?

Or any ideas for my main goal? How to average every two numeric values
ordered by timestamp?


I suspect that it can be done with a single query.

James A. Fortune
CD********@FortuneJames.com

May 12 '06 #3
CD********@FortuneJames.com wrote:
sh********@yahoo.com wrote:
I'm wondernig if it's possible to do the following:

I'd like to average every 2 numeric values in a table as they appear
sequentially ordered by their time stamps. I was thinking the only way
to do this is to create another column in the table that is populated
with sequential numbers that increase by 1 every two values like so:

1
1
2
2
3
3


qryStratify:
SELECT X, ((SELECT Count(A.TimeStamp) FROM tblData AS A WHERE
A.TimeStamp < tblData.TimeStamp)+2)\2 AS Tier FROM tblData ORDER BY
TimeStamp;
And then I can run the average function grouping by this new number
column.


qryAvgX:
SELECT Tier, Avg(X) AS AvgX FROM qryStratify GROUP BY Tier;

Of course, I cannot figure out how to create this customized autonumber
column.

Any ideas?

Or any ideas for my main goal? How to average every two numeric values
ordered by timestamp?


I suspect that it can be done with a single query.

James A. Fortune
CD********@FortuneJames.com


If you have an even number of records, try:

SELECT A.ID, A.X, B.ID, B.X, (A.X + B.X) / 2 AS AvgX FROM tblData AS
A, tblData AS B WHERE (B.TimeStamp = (SELECT Min(C.TimeStamp) FROM
tblData AS C WHERE C.TimeStamp > A.TimeStamp) AND ((SELECT
Count(C.TimeStamp) FROM tblData As C WHERE C.TimeStamp > A.TimeStamp) +
(SELECT Count(*) FROM tblData) + 1) MOD 2 = 0) ORDER BY A.TimeStamp;

It puts the data from the record and the one following together then
skips getting the one following. I haven't figured out a way to get
that last odd record to show up yet.

James A. Fortune
CD********@FortuneJames.com

May 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lauren Quantrell | last post by:
I have a temp table that's populated with an insert query in as tored procedure. The temp table has a uniqueID as the primary key. In that table I have a column SortOrder. What I want to do is to...
9
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has...
7
by: Wolfgang Kreuzer | last post by:
Hello all, I have two tables - Projects and ProjectStruct Table Projects contains master records of the projects, ProjectStruct allows to define a project herarchie and contains the fields...
15
by: alanbe | last post by:
Greetings I am making a flashcard type application to help me in my TCP/IP protocols test. My instructor will test us periodically on how a device or networking function relates to the OSI...
8
by: Donna Sabol | last post by:
First, I should start by saying I am creating a database to be used by some very impatient, non-computer literate people. It needs to be seameless in it's operation from their point of view. I...
3
by: ssb | last post by:
Hello, This may be very elementary, but, need help because I am new to access programming. (1) Say, I have a column EMPLOYEE_NAME. How do I fetch (maybe, cursor ?) the values one by one and...
6
by: Agnes | last post by:
I understand it is impossible, but still curious to know "Can I freeze several column in the datagrid, the user can only scroll the first 3 columns (not verical), for the rest of the coulumn, it is...
1
by: Dale Williams | last post by:
Everyone: Using C#, I'm trying to create a datagrid on a winform programatically. I placed the datagrid and the form and named it CHGrid. Here's the code I'm using: // Get and bind the data....
4
by: mojeza | last post by:
I would like to create generic object which will be used for store of single row of DataTable. Lets say I create class as follow: Public Class Participant Public ParticipantID As Int64 Public...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.