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,
Karen SchaeferBI ANALYSTAsked:
Who is Participating?
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
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
 
Rey Obrero (Capricorn1)Commented:
do you have a unique record Id?
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
Rey Obrero (Capricorn1)Commented:
what is the unique id field name?
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
unique id field name = ID.
0
 
Dany BalianCTOCommented:
can u provide us a sample data, and how do u want the result based on ur sample?
0
 
PatHartmanCommented:
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
 
Karen SchaeferBI ANALYSTAuthor Commented:
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
 
Karen SchaeferBI ANALYSTAuthor Commented:
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
 
Karen SchaeferBI ANALYSTAuthor Commented:
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
 
Rey Obrero (Capricorn1)Commented:
upload a db with your table
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Thanks for the input, disregard the last 2 post, I got it to work.
0
 
PatHartmanCommented:
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
 
Rey Obrero (Capricorn1)Commented:
<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
 
PatHartmanCommented:
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
 
Karen SchaeferBI ANALYSTAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.