Running Row/Record Number (1 Viewer)

Oreynolds

Member
Local time
Today, 21:56
Joined
Apr 11, 2020
Messages
157
Hi, I have a totals query that returns any number of results sorted in date order. What I need is to then create a column called [Sequence] that then shows the row/record sequence number as per below:

YearOrderNumberSumOfQuoteValueSequence
2023MJ00927635987.76
1​
2024MJ00927635987.76
2​
2025MJ00927635987.76
3​

I am sure I am missing something really simple here but I cannot get it to work! Any ideas?

Thanks
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:56
Joined
May 7, 2009
Messages
19,245
as adviced:

SELECT YourTotalQuery.*, (SELECT Count("1") FROM YourTotalQuery AS T WHERE T.Year <= YourTotalQuery.Year) As Sequence
FROM YourTotalQuery Order By [Year].
 

ebs17

Well-known member
Local time
Today, 22:56
Joined
Feb 7, 2020
Messages
1,946
SQL:
SELECT
   T.Year,
   T.OrderNumber,
   T.SumOfQuoteValue,
   DCount("*", "TableX", "Year < " & T.Year) + 1 AS Sequence
FROM
   TableX AS T
DCount is significantly faster here than a comparable subquery.
Because of system-given problems (correlated subquery) one should limit the use to small data sets (number of records < 10000).
If TableX is not a table, but a query with its own effort, one should be even more cautious.


In the constellation shown, year is also already a sequential number. This could be "reformatted".
SQL:
SELECT 
   Year, 
   OrderNumber, 
   SumOfQuoteValue, 
   Year - 
   (
      SELECT 
         MIN(Year) 
      FROM 
         TableX 
   ) 
   + 1 AS Sequence 
FROM 
   TableX
 
Last edited:

Oreynolds

Member
Local time
Today, 21:56
Joined
Apr 11, 2020
Messages
157
as adviced:

SELECT YourTotalQuery.*, (SELECT Count("1") FROM YourTotalQuery AS T WHERE T.Year <= YourTotalQuery.Year) As Sequence
FROM YourTotalQuery Order By [Year].

Hi Sorry, probably being stupid but I cannot get this to work. Here is the SQL from my current query that I want to add another field to called [Sequence], how do I add your script into this query? Thanks

Code:
SELECT qryMaintOrderContractYear.Year, qryMaintOrderContractYear.OrderNumber, Sum(qryMaintOrderContractYear.QuoteValue) AS SumOfQuoteValue
FROM qryMaintOrderContractYear
GROUP BY qryMaintOrderContractYear.Year, qryMaintOrderContractYear.OrderNumber
ORDER BY qryMaintOrderContractYear.Year;
 

Gasman

Enthusiastic Amateur
Local time
Today, 21:56
Joined
Sep 21, 2011
Messages
14,310
You do not. You add the logic into your query.
 

Oreynolds

Member
Local time
Today, 21:56
Joined
Apr 11, 2020
Messages
157
SQL:
SELECT
   T.Year,
   T.OrderNumber,
   T.SumOfQuoteValue,
   DCount("*", "TableX", "Year < " & T.Year) + 1 AS Sequence
FROM
   TableX AS T
DCount is significantly faster here than a comparable subquery.
Because of system-given problems (correlated subquery) one should limit the use to small data sets (number of records < 10000).
If TableX is not a table, but a query with its own effort, one should be even more cautious.


In the constellation shown, year is also already a sequential number. This could be "reformatted".
SQL:
SELECT
   Year,
   OrderNumber,
   SumOfQuoteValue,
   (
      SELECT
         MIN(Year)
      FROM
         TableX
   )
   - Year + 1 AS Sequence
FROM
   TableX

Thanks, tried this as follows:

Code:
SELECT T.Year, T.OrderNumber, T.SumOfQuoteValue, DCount("*","qryMaintOrderContractYearTotals","Year < " & T.Year) AS Sequence
FROM qryMaintOrderContractYearTotals AS T
WHERE (((T.OrderNumber)="MJ0092762"));

But get the following results which aren't right:

Query2 Query2

YearOrderNumberSumOfQuoteValueSequence
2023​
MJ0092762
7421​
7050
2024​
MJ0092762
7421​
7616
2025​
MJ0092762
7421​
7727
Any thoughts?
 

Gasman

Enthusiastic Amateur
Local time
Today, 21:56
Joined
Sep 21, 2011
Messages
14,310
Thanks, tried this as follows:

Code:
SELECT T.Year, T.OrderNumber, T.SumOfQuoteValue, DCount("*","qryMaintOrderContractYearTotals","Year < " & T.Year) AS Sequence
FROM qryMaintOrderContractYearTotals AS T
WHERE (((T.OrderNumber)="MJ0092762"));

But get the following results which aren't right:

Query2 Query2

YearOrderNumberSumOfQuoteValueSequence
2023​
MJ0092762
7421​
7050
2024​
MJ0092762
7421​
7616
2025​
MJ0092762
7421​
7727
Any thoughts?
I suspect you need to use the same criteria in your Dcount/subquery?
 

ebs17

Well-known member
Local time
Today, 22:56
Joined
Feb 7, 2020
Messages
1,946
SQL:
SELECT
   T.Year,
   T.OrderNumber,
   T.SumOfQuoteValue,
   DCount("*", " qryMaintOrderContractYearTotals", "OrderNumber = '" & T.OrderNumber & "' AND Year < " & T.Year) + 1 AS Sequence
FROM
   qryMaintOrderContractYearTotals AS T
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:56
Joined
May 7, 2009
Messages
19,245
Code:
SELECT qryMaintOrderContractYearTotals.*,
(SELECT Count("1") FROM qryMaintOrderContractYearTotals AS T WHERE T.Year <= qryMaintOrderContractYearTotals.Year) As Sequence
FROM qryMaintOrderContractYearTotals Order By [Year]
 

ebs17

Well-known member
Local time
Today, 22:56
Joined
Feb 7, 2020
Messages
1,946
Or as an implementation of the second thought above:
SQL:
SELECT
   M.Year,
   M.OrderNumber,
   SUM(M.QuoteValue) AS SumOfQuoteValue,
   M.Year -
   (
      SELECT
         MIN(X.Year)
      FROM
         qryMaintOrderContractYear AS X
      WHERE
         X.OrderNumber = M.OrderNumber
   )
   + 1 AS Sequence
FROM
   qryMaintOrderContractYear AS M
GROUP BY
   M.Year,
   M.OrderNumber
ORDER BY
   M.Year
 

Oreynolds

Member
Local time
Today, 21:56
Joined
Apr 11, 2020
Messages
157
Code:
SELECT qryMaintOrderContractYearTotals.*,
(SELECT Count("1") FROM qryMaintOrderContractYearTotals AS T WHERE T.Year <= qryMaintOrderContractYearTotals.Year) As Sequence
FROM qryMaintOrderContractYearTotals Order By [Year]

Thankyou, all sorted
 

Users who are viewing this thread

Top Bottom