Solved

Create Counter based on Grouping and Sorted Order

Posted on 2014-11-12
11
166 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 35

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 33

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 33

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 33

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 33

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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.

832 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