Solved Loop de Loop (1 Viewer)

Space Cowboy

Member
Local time
Today, 01:25
Joined
May 19, 2024
Messages
167
clearer CODE.

SELECT ftq1.odate,
filtertable1.orderid,
ftq1.sumofunitprice,
Dcount("*", "ftq1", "sumofunitprice>=" & [sumofunitprice]) AS DayRank,
filtertable1.unitprice,
Dcount("*", "filtertable1", "unitprice>=" & [unitprice] &
"and orderid='" &
[filtertable1].[orderid] & "'") AS PriceRank
FROM filtertable1
INNER JOIN ftq1
ON filtertable1.orderid = ftq1.orderid
WHERE (( ( ftq1.odate ) = #5/27/2024# ))
ORDER BY Dcount("*", "ftq1", "sumofunitprice>=" & [sumofunitprice]),
filtertable1.unitprice,
Dcount("*", "filtertable1", "unitprice>=" & [unitprice] &
"and orderid='" &
[filtertable1].[orderid] & "'");
 

cheekybuddha

AWF VIP
Local time
Today, 01:25
Joined
Jul 21, 2014
Messages
2,412
First try this:
SQL:
SELECT
  ftq1.odate,
  filtertable1.orderid,
  ftq1.sumofunitprice,
  Dcount("*", "ftq1", "sumofunitprice>=" & [sumofunitprice]) AS DayRank,
  filtertable1.unitprice
FROM filtertable1
INNER JOIN ftq1
        ON filtertable1.orderid = ftq1.orderid
WHERE ftq1.odate = #2024-05-27#
ORDER BY
  Dcount("*", "ftq1", "sumofunitprice>=" & [sumofunitprice]),
  filtertable1.unitprice DESC
;

Let us know whether your 'Needed' column (UnitPrice) shows in the correct order with all your other data.

Then you can work on the ranking

(I probably won't be able to look in again here until tomorrow - others may well pitch in before then)
 

Space Cowboy

Member
Local time
Today, 01:25
Joined
May 19, 2024
Messages
167
First try this:
SQL:
SELECT
  ftq1.odate,
  filtertable1.orderid,
  ftq1.sumofunitprice,
  Dcount("*", "ftq1", "sumofunitprice>=" & [sumofunitprice]) AS DayRank,
  filtertable1.unitprice
FROM filtertable1
INNER JOIN ftq1
        ON filtertable1.orderid = ftq1.orderid
WHERE ftq1.odate = #2024-05-27#
ORDER BY
  Dcount("*", "ftq1", "sumofunitprice>=" & [sumofunitprice]),
  filtertable1.unitprice DESC
;

Let us know whether your 'Needed' column (UnitPrice) shows in the correct order with all your other data.

Then you can work on the ranking

(I probably won't be able to look in again here until tomorrow - others may well pitch in before then)
Yes that works on unit price, I had it like that using DESC. However the dayrank has gone to pot.

dataFTQ2Ping2.PNG
 

Space Cowboy

Member
Local time
Today, 01:25
Joined
May 19, 2024
Messages
167
The day rank messes up as soon as the data table includes extra days.

The extra dates taken out.

dataFTQ2Ping3.PNG
 

cheekybuddha

AWF VIP
Local time
Today, 01:25
Joined
Jul 21, 2014
Messages
2,412
The day rank messes up as soon as the data table includes extra days.

So you need to add the date criteria to your DCount() as well:
SQL:
SELECT
  ftq1.odate,
  filtertable1.orderid,
  ftq1.sumofunitprice,
  DCount("*", "ftq1", "sumofunitprice >= " & [sumofunitprice] & " AND odate = #2024-05-27#") AS DayRank,
  filtertable1.unitprice
FROM filtertable1
INNER JOIN ftq1
        ON filtertable1.orderid = ftq1.orderid
WHERE ftq1.odate = #2024-05-27#
ORDER BY
  DCount("*", "ftq1", "sumofunitprice >= " & [sumofunitprice] & " AND odate = #2024-05-27#"),
  filtertable1.unitprice DESC
;
 

cheekybuddha

AWF VIP
Local time
Today, 01:25
Joined
Jul 21, 2014
Messages
2,412
Including PriceRank might look like this:
SQL:
SELECT
  ftq1.odate,
  filtertable1.orderid,
  ftq1.sumofunitprice,
  DCount("*", "ftq1", "sumofunitprice >= " & [sumofunitprice] & " AND odate = #2024-05-27#") AS DayRank,
  filtertable1.unitprice,
  DCount("*", "filtertable1", "unitprice <= " & filtertable1.unitprice & " AND odate = #2024-05-27#") AS PriceRank
FROM filtertable1
INNER JOIN ftq1
        ON filtertable1.orderid = ftq1.orderid
WHERE ftq1.odate = #2024-05-27#
ORDER BY
  DCount("*", "ftq1", "sumofunitprice >= " & [sumofunitprice] & " AND odate = #2024-05-27#"),
  filtertable1.unitprice DESC
;
 

Space Cowboy

Member
Local time
Today, 01:25
Joined
May 19, 2024
Messages
167
Good Morning Good People,

@cheekybuddha
What had I done wrong to cause the data mismatch error? I am trying to understand and Learn
I think that I have called a number field a string where as CJ original code the field was a text field

cname='
OrderId='

does this act differently on a text or number field?
What is the apostrophe doing?
 
Last edited:

cheekybuddha

AWF VIP
Local time
Today, 01:25
Joined
Jul 21, 2014
Messages
2,412
Your original expression has 2 (perhaps 3) issues:
Code:
Dcount("*", "filtertable1", "unitprice>=" & [unitprice] & "and orderid='" & [filtertable1].[orderid] & "'") AS PriceRank

First, you need to add a space between the value you inject for UnitPrice and the 'AND' of the next part of the criteria clause:
Code:
Dcount("*", "filtertable1", "unitprice>=" & [unitprice] & " and orderid='" & [filtertable1].[orderid] & "'") AS PriceRank
                                                           ^
                                                           |
                                                  space reuired here

Then, as you have guessed, with the addition of quotes around the value, you are passing orderid as a string rather than a number in the second part of the criteria clause:
Code:
Dcount("*", "filtertable1", "unitprice>=" & [unitprice] & "and orderid='" & [filtertable1].[orderid] & "'") AS PriceRank
                                                                       ^                             ^^^^^
                                                                       |                             |||||
                                                                Use these to wrap string values, not numbers

Finally, in order to reverse the ranking consistent with the UnitPrice ordering, you might need to change the comparison order for UnitPrice:
Code:
Dcount("*", "filtertable1", "unitprice>=" & [unitprice] & " and orderid= " & [filtertable1].[orderid]) AS PriceRank
                                      ^^
                                      ||
                        se Less Than, rather than Greater Than

So I guess you want:
Code:
Dcount("*", "filtertable1", "unitprice <= " & filtertable1.unitprice & " AND orderid= " & filtertable1.orderid) AS PriceRank

In your query:
SQL:
SELECT
  ftq1.odate,
  filtertable1.orderid,
  ftq1.sumofunitprice,
  DCount("*", "ftq1", "sumofunitprice >= " & ftq1.sumofunitprice & " AND odate = #2024-05-27#") AS DayRank,
  filtertable1.unitprice,
  Dcount("*", "filtertable1", "unitprice <= " & filtertable1.unitprice & " AND orderid= " & filtertable1.orderid) AS PriceRank
FROM filtertable1
INNER JOIN ftq1
        ON filtertable1.orderid = ftq1.orderid
WHERE ftq1.odate = #2024-05-27#
ORDER BY
  DCount("*", "ftq1", "sumofunitprice >= " & ftq1.sumofunitprice & " AND odate = #2024-05-27#"),
  filtertable1.unitprice DESC
;
 
Last edited:

cheekybuddha

AWF VIP
Local time
Today, 01:25
Joined
Jul 21, 2014
Messages
2,412
Other things to note:

Use of domain aggregate functions (DCount(), DSum(), DLookup(), etc) within a query can have a heavy performance impact if the the query returns many rows.

You don't say where you will use this query.

If it is for use on a report, then using built-in report features can be more performant to create rankings then creating the ranking in the underlying recordset.
 

Space Cowboy

Member
Local time
Today, 01:25
Joined
May 19, 2024
Messages
167
ta da.........
dataFTQ2Ping4.PNG


Now for the tricky part, moving to the next day (28/5/2024) and then performing that same operation on that day.
 

Space Cowboy

Member
Local time
Today, 01:25
Joined
May 19, 2024
Messages
167
Other things to note:

Use of domain aggregate functions (DCount(), DSum(), DLookup(), etc) within a query can have a heavy performance impact if the the query returns many rows.

You don't say where you will use this query.

If it is for use on a report, then using built-in report features can be more performant to create rankings then creating the ranking in the underlying recordset.
Hi David,
this is the foundation of a query that will have approx 20 other fields attached to it and will go back 4 or 5 years. I think there will be about 70,000 rows at the end of it. I will then transform to a table where I can perform other queries and generate more specific reports.

I cant make any real progress until I have this sorted out.

Thank you once again my friend.
 

ebs17

Well-known member
Local time
Today, 02:25
Joined
Feb 7, 2020
Messages
2,081
Code:
Dcount("*", "filtertable1", "unitprice <= " & filtertable1.unitprice & " AND orderid= " & filtertable1.orderid) AS PriceRank
This becomes, when viewed daily:
Code:
Dcount("*", "filtertable1", "unitprice <= " & filtertable1.unitprice & " AND orderid= " & filtertable1.orderid & " AND odate = " & Format(filtertable1.odate, "\#yyyy\-mm\-dd\#")) AS PriceRank

I think there will be about 70,000 rows at the end of it
The calculation of PriceRank via DCount has a systematic performance problem. My personal limit is 10,000 records, after which the runtime becomes unbearable.
 

Space Cowboy

Member
Local time
Today, 01:25
Joined
May 19, 2024
Messages
167
@ebs17
Thank you so much for that! I have transposed the format section from that line into the Dayrank calculation also and it has worked beautifully. I cannot believe how powerful Access is in only a few lines of code and in only a few words really.
dataFTQ2Ping5.PNG

To You all, a sincere thank you. The calculations and formatting that you have all helped with in this example will be used by me in different methods in lots of query types, I just hope that my efforts will do your kind and thoughtful approach justice.
 

Users who are viewing this thread

Top Bottom