Hello,
I am running into issues combing the UNION SELECT and ORDER BY commands in Access SQL. My two select statements select from the same table and both utilize SELECT *. The WHERE conditions on each of these SELECT's are slightly different and call for two separate queries. I then use one of the fields from the SELECT statement to customize the ordering of the query.
Code:
SELECT *
FROM [tblStatus]
WHERE Program = 'ABC'
AND (StartDate BETWEEN #8/29/2017# AND #8/31/2017# OR Ongoing = True)
AND
(
OGAuthor IN
(
SELECT LeadName
FROM tblLead
WHERE tblLead.Program = 'ABC'
And Department = 'SW'
)
OR OGAuthor IN
(
SELECT FullName
FROM tblUsers
WHERE tblUsers.Department = 'SW'
)
)
AND Urgent = True
AND isCurrent = True
UNION SELECT *
FROM [tblStatus]
WHERE OGAuthor = 'John Smith'
AND (StartDate BETWEEN #8/29/2017# AND #8/31/2017# OR Ongoing = True)
AND Urgent = True
AND isCurrent = True
ORDER BY IIf([Program] Like '*General', 1, 0) DESC, Program, SubProgram, Task, Initials DESC, StartDate DESC, OGStartDate DESC, ID ASC;
I believe the problem is coming from the bit of logic in my ORDER BY clause. If I remove the "IIf" logic from the ORDER BY statement, it works. However, I really want to use that bit because it's better (to me) to use that instead of introducing an entirely new field just for ordering based on the likeliness to "*General" Currently, I get the following error.
Runtime Error: 3351
The ORDER BY expression (IIf([Program] Like '*General',1,0))) includes fields that are not selected by the query. Only those fields requested in the first query can be included in a ORDER BY expression.
This is strange to me because 'Program' is included in both queries. So this error is messing me up.
Additional Articles:
https://www.pcreview.co.uk/threads/u...-work.1165529/
https://www.accessforums.net/showthr...723#post368723