Unable to Sort on calculated field, based on another calculated field (1 Viewer)

JohnPapa

Registered User.
Local time
Today, 11:43
Joined
Aug 15, 2010
Messages
954
I have the following RecordSource

Code:
SELECT dbo_tblManagementCommitteeInstance.intID, dbo_tblManagementCommitteeInstance.intManagementCommittee, dbo_tblManagementCommitteeInstance.dtmFrom,
dbo_tblManagementCommitteeInstance.dtmTo, dbo_tblManagementCommitteeInstance.bitD, dbo_tblManagementCommitteeInstance.dtmD, dbo_tblManagementCommitteeInstance.nvcLUC,
dbo_tblManagementCommitteeInstance.intD, dbo_tblIe.nvcName, dbo_tblIe.intMCTerm, dbo_tblIe.intMCTermAlert, DateAdd("m",[intMCTerm],[dtmFrom]) AS dtmExpiry,
DateAdd("m",-[intMCTermAlert],[dtmExpiry]) AS dtmExpiryAlert, DateDiff("d",Date(),[dtmExpiry]) AS intExpiryDay
FROM dbo_tblIe LEFT JOIN dbo_tblManagementCommitteeInstance ON dbo_tblIe.intID = dbo_tblManagementCommitteeInstance.intManagementCommittee
WHERE (((dbo_tblManagementCommitteeInstance.bitD)=False)) ;

and would like to Sort on calculated field intExpiryDay.

When I try to use

Code:
ORDER BY intExpiryDay

it is prompting me to enter a value for Parameter "dtmExpiry", which is used in intExpiryDay

Code:
DateDiff("d",Date(),[dtmExpiry]) AS intExpiryDay
 

ebs17

Well-known member
Local time
Today, 10:43
Joined
Feb 7, 2020
Messages
1,946
The order of query execution is not from front to back, but has its own rules.

To shorten it: When sorting, the calculated field has not yet been named, so the given alias intExpiryDay is not yet known. This is why the parameter demand also occurs.

At this point you would have to repeat the calculation expression:
SQL:
...
ORDER BY DateDiff("d",Date(),[dtmExpiry])
 

JohnPapa

Registered User.
Local time
Today, 11:43
Joined
Aug 15, 2010
Messages
954
The order of query execution is not from front to back, but has its own rules.

To shorten it: When sorting, the calculated field has not yet been named, so the given alias intExpiryDay is not yet known. This is why the parameter demand also occurs.

At this point you would have to repeat the calculation expression:
SQL:
...
ORDER BY DateDiff("d",Date(),[dtmExpiry])
I had already tried your suggestion and it tells me "Syntax error in ORDER BY clause".

BTW, the BE is SQL Server.
 

ebs17

Well-known member
Local time
Today, 10:43
Joined
Feb 7, 2020
Messages
1,946
For Jet-SQL the expression shown is correct.

Where exactly is the query being executed? T-SQL obviously requires a slightly different syntax.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 09:43
Joined
Feb 19, 2013
Messages
16,612
think ebd17 meant

ORDER BY DateDiff("d",Date(),DateAdd("m",[intMCTerm],[dtmFrom]))

since [dtmExpiry] is also a calculated value
 

Josef P.

Well-known member
Local time
Today, 10:43
Joined
Feb 2, 2023
Messages
826
Alternative: Sort by column number:
Code:
SELECT
   dbo_tblManagementCommitteeInstance.intID,
   dbo_tblManagementCommitteeInstance.intManagementCommittee,
   dbo_tblManagementCommitteeInstance.dtmFrom,
   dbo_tblManagementCommitteeInstance.dtmTo,
   dbo_tblManagementCommitteeInstance.bitD,
   dbo_tblManagementCommitteeInstance.dtmD,
   dbo_tblManagementCommitteeInstance.nvcLUC,
   dbo_tblManagementCommitteeInstance.intD,
   dbo_tblIe.nvcName,
   dbo_tblIe.intMCTerm,
   dbo_tblIe.intMCTermAlert,
   DateAdd("m",[intMCTerm],[dtmFrom]) AS dtmExpiry,
   DateAdd("m",-[intMCTermAlert],[dtmExpiry]) AS dtmExpiryAlert,
   DateDiff("d",Date(),[dtmExpiry]) AS intExpiryDay
FROM
   dbo_tblIe LEFT JOIN dbo_tblManagementCommitteeInstance ON dbo_tblIe.intID = dbo_tblManagementCommitteeInstance.intManagementCommittee
WHERE
   (((dbo_tblManagementCommitteeInstance.bitD)=False))
Order By
    14
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:43
Joined
Sep 21, 2011
Messages
14,301
Thanks. I had overlooked that.
Well if you think you had to replace a calculated value with the calculation for one, you would need to do the same for any others? :unsure:
 

ebs17

Well-known member
Local time
Today, 10:43
Joined
Feb 7, 2020
Messages
1,946
Yes, depending on the location of use.
Filtering in the WHERE clause and then grouping take place before sorting and therefore cannot use a column alias that is created in the SELECT part.
 

JohnPapa

Registered User.
Local time
Today, 11:43
Joined
Aug 15, 2010
Messages
954
Alternative: Sort by column number:
Code:
SELECT
   dbo_tblManagementCommitteeInstance.intID,
   dbo_tblManagementCommitteeInstance.intManagementCommittee,
   dbo_tblManagementCommitteeInstance.dtmFrom,
   dbo_tblManagementCommitteeInstance.dtmTo,
   dbo_tblManagementCommitteeInstance.bitD,
   dbo_tblManagementCommitteeInstance.dtmD,
   dbo_tblManagementCommitteeInstance.nvcLUC,
   dbo_tblManagementCommitteeInstance.intD,
   dbo_tblIe.nvcName,
   dbo_tblIe.intMCTerm,
   dbo_tblIe.intMCTermAlert,
   DateAdd("m",[intMCTerm],[dtmFrom]) AS dtmExpiry,
   DateAdd("m",-[intMCTermAlert],[dtmExpiry]) AS dtmExpiryAlert,
   DateDiff("d",Date(),[dtmExpiry]) AS intExpiryDay
FROM
   dbo_tblIe LEFT JOIN dbo_tblManagementCommitteeInstance ON dbo_tblIe.intID = dbo_tblManagementCommitteeInstance.intManagementCommittee
WHERE
   (((dbo_tblManagementCommitteeInstance.bitD)=False))
Order By
    14
I can confirm that this works as well. Did not know that you could just insert the column number like that.
 

Users who are viewing this thread

Top Bottom