Link to home
Start Free TrialLog in
Avatar of Karen Schaefer
Karen SchaeferFlag for United States of America

asked on

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

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,
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

do you have a unique record Id?
Avatar of Karen Schaefer

ASKER

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
what is the unique id field name?
unique id field name = ID.
can u provide us a sample data, and how do u want the result based on ur sample?
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Pat, Thanks for the suggestion, however, I rethought this issue and I will need to actually assign a value to the underlining table.

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

upload a db with your table
Thanks for the input, disregard the last 2 post, I got it to work.
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.
<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
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;
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