Solved

Create Counter based on Grouping and Sorted Order

Posted on 2014-11-12
11
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 37

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 500 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

738 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