Solved

Create Counter based on Grouping and Sorted Order

Posted on 2014-11-12
11
171 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 36

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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

828 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