Solved

Create Counter based on Grouping and Sorted Order

Posted on 2014-11-12
11
161 Views
Last Modified: 2014-11-13
I'm currently trying to create a new "counter" column in Access for my Data set based on two fields. My dataset looks something like this:

Date, Time
12/01/2014, 4:00:01
12/01/2014, 4:00:01
12/01/2014, 4:00:03
12/02/2014, 2:24:01
12/02/2014, 2:25:01
12/02/2014, 2:26:01

I would like to create a counter column that restarts at 1 every day, increasing 1+ based on the sorted order of Time.

Date, Time, counter
12/01/2014, 4:00:01,1
12/01/2014, 4:00:01,2
12/01/2014, 4:00:03,3
12/02/2014, 2:24:01,1
12/02/2014, 2:25:01,2
12/02/2014, 2:26:01,3

Tried a number of things that didn't work: can't use rank because there are multiple records for one "time".

Thanks in advance for your help!

Cathy
0
Comment
Question by:cathyha
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
Why did you start a new thread?
0
 

Author Comment

by:cathyha
Comment Utility
There wasn't an option to edit my question
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
you can use a query to do this, provided you have a unique id field.
0
 

Author Comment

by:cathyha
Comment Utility
Yes there is a unique ID field. Can you be a little more specific please with the query? I have no idea how to even begin.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
what are the name of the table, fields (exact names)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:cathyha
Comment Utility
The table name is Weights

Fields are ID, Date, Time, Weight.
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
Comment Utility
Please note that IDs and Date_Time must have the same Sorting (Asc) in the original Weights table.
SELECT t.ID, t.[Date], t.[Time], (Select Count(tt.Time)  From Weights tt Where t.[Date]=tt.[Date] and t.ID>tt.ID)+1 AS No
FROM Weights AS t
ORDER BY t.ID

Open in new window


ID	Date	        Time    No
1	12/1/2014	4:00:01	1
2	12/1/2014	4:00:01	2
3	12/1/2014	4:00:03	3
4	12/2/2014	4:00:01	1
5	12/2/2014	4:00:01	2
6	12/2/2014	4:00:01	3

Open in new window

cathyha, I see that you are a new member. Welcome to EE. Also, before deleting your question, please let the experts know this is what you intend to do first. Otherwise you leave experts wondering what happened.

Thanks,

Mike
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
This will work too:
SELECT t.ID, t.[Date], t.[Time], (Select Count(tt.Time)  From Weights tt Where t.[Date]=tt.[Date] and t.ID>=tt.ID) AS No
FROM Weights AS t
ORDER BY t.ID

Open in new window

0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
cathyha,

How did it work out?

Mike
0
 

Author Comment

by:cathyha
Comment Utility
Ended up using a combination of a SQL query and public function to do this:

Option Compare Database

Global GBL_Date As Long
Global GBL_Icount As Long

Public Function increment(ivalue As Long) As Long

If Nz(GBL_Date) = ivalue Then
        GBL_Icount = GBL_Icount + 1
Else
        GBL_Date = ivalue
        GBL_Icount = 1
End If
increment = GBL_Icount
End Function

Open in new window


SQL query to insert the count:
UPDATE Table SET Counter = increment([DateOnly]);

Open in new window


Thanks for the help!
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
Didn't the query work for you? If it didn't, I think it will have a better performance.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now