tags:

views:

195

answers:

1

In my query, I use the IIF function to assign either "Before" or "After" to a field named BeforeOrAfter using AS.

When I run this query, however, the "Enter Parameter Value" dialog appears, requesting a value for BeforeOrAfter. If I remove BeforeOrAfter DESC from the ORDER BY clause, I don't get the dialog.

Here is the offending query:

SELECT
    d.Scenario,
    e.Event,
    IIF(d.LogTime < e.Time, 'Before','After') AS BeforeOrAfter,
    d.HeartRate
FROM
    Data d INNER JOIN
    Events e ON d.Scenario = e.Scenario
WHERE
    e.Include = Yes
ORDER BY
    d.Scenario,
    e.Id,
    BeforeOrAfter DESC

Question: Why is my AS BeforeOrAfter not being recognized by the ORDER BY clause? Why does it ask me to enter a parameter value for "BeforeOrAfter" when I run this query?

Note: I tried using brackets, single quotes, double quotes, etc., but none of that made any difference.

+4  A: 

I beleive Access can't handle the alias feature, so you'll have to copy your IIF-block down into the Order By -clause. Or create a subquery (and then you might even find yourself forced to even not be able to use parantheses if your Access version isn't among the newest two or so).

Simon B.
SELECT d.Scenario, e.Event, IIF(d.LogTime < e.Time, 'Before','After') AS BeforeOrAfter, d.HeartRateFROM Data d INNER JOIN Events e ON d.Scenario = e.ScenarioWHERE e.Include = YesORDER BY d.Scenario, e.Id, IIF(d.LogTime < e.Time, 'Before','After') DESC
clyc
Thanks, Simon. This is my first time writing queries in Access. I'm quickly learning that it's quite different than SQL Server.
DanM
Why would you not expect a different database engine to have a different SQL dialect? For what it's worth, you might find some things easier if you set Access to use "SQL 92" mode, which changes a handful of things to be compatible with SQL Server's SQL dialect (the LIKE wildcards and the syntax for derived tables are the only ones I can think of).
David-W-Fenton
DanM
David-W-Fenton