By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.