Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create Counter based on Grouping and Sorted Order

Posted on 2014-11-12
11
Medium Priority
?
187 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 40

Expert Comment

by:PatHartman
ID: 40438440
Why did you start a new thread?
0
 

Author Comment

by:cathyha
ID: 40438448
There wasn't an option to edit my question
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40438511
you can use a query to do this, provided you have a unique id field.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:cathyha
ID: 40438515
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40438534
what are the name of the table, fields (exact names)
0
 

Author Comment

by:cathyha
ID: 40438544
The table name is Weights

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

Accepted Solution

by:
Mike Eghtebas earned 2000 total points
ID: 40438608
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 34

Expert Comment

by:Mike Eghtebas
ID: 40438664
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 34

Expert Comment

by:Mike Eghtebas
ID: 40438849
cathyha,

How did it work out?

Mike
0
 

Author Comment

by:cathyha
ID: 40438970
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 34

Expert Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

783 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