Why does this query require a parameter?

℡╲_俬逩灬. 提交于 2020-02-23 10:27:26

问题


I have the following SQL query in MS Access. I'm trying to order the output by the results of an expression contained in the column labeled as 'Response'. My problem is that when I run the query Access prompts me to enter a parameter value for Response. I tried entering zero and one to see what would happen. The query runs in those cases but the sort order is wrong. Can someone please explain to me why this query would require a parameter? Am I doing something wrong?

SELECT Market,
       Sum(Calls) AS SumOfCalls,
       Sum([A25-54 IMPs] * 1000) AS Impressions,
       Round(SumOfCalls/Impressions, 6) AS Response
FROM DRTV_CentralOnly
WHERE [Creative]<>'#N/A'
GROUP BY Market
ORDER BY Response Desc;

回答1:


It is not requiring a parameter. The problem is that you are using columns aliases in one your Response column. You need to use the actual calculations instead.

SELECT Market
    , Sum(Calls) AS SumOfCalls
    , Sum([A25-54 IMPs] * 1000) AS Impressions
    , Round(Sum(Calls)/Sum([A25-54 IMPs] * 1000), 6) AS Response
FROM DRTV_CentralOnly
WHERE [Creative]<>'#N/A'
GROUP BY Market
ORDER BY 4 Desc;



回答2:


@bluefeet said:

The problem is that you are using columns aliases in one your Response column.

Actually, that is not the problem at all. MS Access does indeed allow AS clauses ("columns aliases") to be used in the way the OP has used them.

Rather, the problem is that MS Access does not allow AS clauses in the ORDER BY clause.

It is altering the ORDER BY clause to use an ordinal position that fixes the query. The changes to the SELECT clause are a red herring!

The following should work:

SELECT Market,
       Sum(Calls) AS SumOfCalls,
       Sum([A25-54 IMPs] * 1000) AS Impressions,
       Round(SumOfCalls/Impressions, 6) AS Response
FROM DRTV_CentralOnly
WHERE [Creative]<>'#N/A'
GROUP BY Market
ORDER BY 4 Desc;


来源:https://stackoverflow.com/questions/9705176/why-does-this-query-require-a-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!