Solved

set record number based on grouping in a query (recordcount)  Looking for function

Posted on 2015-01-26
16
107 Views
Last Modified: 2015-01-28
Need to set a sequential numbering per a grouping from within a query.

Data = ReqNO = 1
IDNo

So
ReqNo = 1, IdNo 1
ReqNo = 1, IdNo 2
ReqNo = 1, IdNo 3
ReqNo = 1, IdNo 4
next
ReqNo = 2, IdNo 1
ReqNo = 2, IdNo 2
ReqNo = 3, IdNo 1
ReqNo = 3, IdNo2

Looking for a function I can call from within the report's datasource query.

Thanks,
0
Comment
Question by:Karen Schaefer
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40571505
do you have a unique record Id?
0
 

Author Comment

by:Karen Schaefer
ID: 40571519
Yes I need to have separate numbering that restarts with the next group hence.  

Example

ReqNo = 1, IdNo 1
ReqNo = 1, IdNo 2
ReqNo = 1, IdNo 3
ReqNo = 1, IdNo 4
next
ReqNo = 2, IdNo = 1
ReqNo = 2, IdNo = 2
ReqNo = 3, IdNo = 1
ReqNo = 3, IdNo=2
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40571522
what is the unique id field name?
0
 

Author Comment

by:Karen Schaefer
ID: 40571523
unique id field name = ID.
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40571537
can u provide us a sample data, and how do u want the result based on ur sample?
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 40571541
select Id,ReqNo,(select count(*) from tableName where reqno= a.reqno and id <= a.id ) as IdNo
from TableName as a

change "TableName" with actual name of table
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40571634
Since you are doing this in a report, there is no need to run a separate count query for every single record in the recordsource.  Simply add a control with a controlSource of:
=1
Then set its RunningSum property to OverGroup or OverAll.
0
 

Author Comment

by:Karen Schaefer
ID: 40573046
Pat, Thanks for the suggestion, however, I rethought this issue and I will need to actually assign a value to the underlining table.

K
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:Karen Schaefer
ID: 40573087
Rey,

Thanks for the input, I tried your query, however the numbering is not working correctly,  for some reason, some of the ID's return the value of 3 when it should be 1.

SELECT a.ReqNo, a.Id, (select count(*) from DBAQuestions where reqno= a.reqno and id <= a.id ) AS QNo
FROM DBAQuestions AS a
ORDER BY a.ReqNo;

Open in new window

screenshot.png
0
 

Author Comment

by:Karen Schaefer
ID: 40573093
Sorry posted wrong query
SELECT a.ReqNo, a.Id, (select count(*) from DBAQuestions where reqno= a.reqno and id <= a.id ) AS QNo
FROM DBAQuestions AS a
ORDER BY a.ReqNo;

Open in new window

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40573116
upload a db with your table
0
 

Author Closing Comment

by:Karen Schaefer
ID: 40573132
Thanks for the input, disregard the last 2 post, I got it to work.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40573884
I rethought this issue and I will need to actually assign a value to the underlining table.
Just make sure you understand that the query method you have elected to use is the worst of all possible solutions.  It must run a separate query for each row returned by the main query.  So if the query returns 1000 rows, then 1000 additional queries must run to return the count for each record.  If the query returns 10,000 records then 10,000!!!! queries must run to return the count.  The more rows the main query returns, the slower this process becomes.  The method I suggested for use in the report doesn't require running ANY additional queries since the report processes the query as a sequential recordset and so can keep track of where it is and count and break as it formats the report.  If it is necessary to assign the sequence number permanently, then the best solution is to do it in the form that adds the record.  That way it is unchanging and you only have to calculate it once.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40573929
<Just make sure you understand that the query method you have elected to use is the worst of all possible solutions.>

????

<The method I suggested for use in the report doesn't require running ANY additional queries ..>

True, but

can you use that method to assign the sequence number permanently ? NO

< then the best solution is to do it in the form that adds the record.>

how about the existing records that, how will you update them?
>> use the query
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40574011
Rey,
I think you misunderstood my objection.  It wasn't a criticism of you.  The query you suggested does do what the OP requested and the technique does work, to a point.  You answered the question literally.  The OP specifically asked for a query.   That was very likely because he didn't know that he had other options.  I was pointing out two other options, both of which would be less "expensive" than the query solution.

If the objective is to update the existing records, you have to bite the bullet and run a query similar to the one you posted but the query you posted wasn't an update query.  It was a select query so the implication is that the numbers are generated again every time the query runs.  As long as the recordset is small, it's no big deal but once you start running thousands of queries (remember - it's a separate query for EACH row), you start taking a long time each and every time the query runs.  If the only reason for the numbers is to number items in a printed report, then the method I suggested is the way to go since it doesn't degrade the process.  If the sequence number is permanent, then it should be assigned when the record is added.  

I was trying to convince the OP to either assign the number permanently or let the report assign the number as part of the printing process.  Assigning potentially new numbers each time a query runs is the least efficient of the three options.  And as the recordset gets larger, the length of time to actually run the query will be longer than any user will be happy with.

There is also a second query option which is more efficient than the extra query per row method but it does result in a non-updateable recordset.  So, if the recordset doesn't need to be updateable, I would use a self join.  The query joins a table to itself using a non-equi-join so it has to be created in SQL view and can never be displayed in QBE view.

Select GroupID, fld1, fld2, Count(*) as Seq
From tblA Inner Join tblA_1 on tblA.GroupID = tblA_1.GroupID AND tblA_1.PK < tblA.PK
Group By GroupID, fld1, fld2;
0
 

Author Comment

by:Karen Schaefer
ID: 40575786
Thanks for all the input, the query did the job I just needed to create the initial numbering. I used the sql as part of a update query and it worked great.  it was just a one off situation.

Thanks again.
Karen
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email from List Data 2 51
sort Time by AM and PM in query 2 20
running an update statement after a msgbox (MS Access) 3 31
ms access 2013, running .mdb 2 31
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

895 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

17 Experts available now in Live!

Get 1:1 Help Now