Running Row/Record Number

Oreynolds

Member
Local time
Today, 06:33
Joined
Apr 11, 2020
Messages
165
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
 
as adviced:

SELECT YourTotalQuery.*, (SELECT Count("1") FROM YourTotalQuery AS T WHERE T.Year <= YourTotalQuery.Year) As Sequence
FROM YourTotalQuery Order By [Year].
 
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:
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;
 
You do not. You add the logic into your query.
 
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?
 
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?
 
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:
Code:
SELECT qryMaintOrderContractYearTotals.*,
(SELECT Count("1") FROM qryMaintOrderContractYearTotals AS T WHERE T.Year <= qryMaintOrderContractYearTotals.Year) As Sequence
FROM qryMaintOrderContractYearTotals Order By [Year]
 
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
 
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

Back
Top Bottom