MS-Access->选择为 +排序依据 = 错误
我正在尝试进行查询以检索甜品销量最高的地区。 “grupo_produto”是产品类型,“regiao”是区域。所以我得到了这个查询:
SELECT TOP 1 r.nm_regiao, (SELECT COUNT(*)
FROM Dw_Empresa
WHERE grupo_produto='1' AND
cod_regiao = d.cod_regiao) as total
FROM Dw_Empresa d
INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao ORDER BY total DESC
然后,当我运行查询时,MS-Access 要求提供“total”参数。为什么它不考虑我在 select 子句中创建的新创建的“列”?
提前致谢!
I'm trying to make a query to retrieve the region which got the most sales for sweet products. 'grupo_produto' is the product type, and 'regiao' is the region. So I got this query:
SELECT TOP 1 r.nm_regiao, (SELECT COUNT(*)
FROM Dw_Empresa
WHERE grupo_produto='1' AND
cod_regiao = d.cod_regiao) as total
FROM Dw_Empresa d
INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao ORDER BY total DESC
Then when i run the query, MS-Access asks for the "total" parameter. Why it doesn't consider the newly created 'column' I made in the select clause?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我知道老问题,但它可能会帮助某人了解,虽然你不能按别名排序,但你可以按列索引排序。例如,这将不会出现错误:
然后,结果将按第二列中找到的值(别名“yourAlias”)进行排序。
Old Question I know, but it may help someone knowing than while you cant order by aliases, you can order by column index. For example, this will work without error :
The results would then be ordered by the values found in the second column wich is the Alias "yourAlias".
别名只能在查询输出中使用。您不能在查询的其他部分使用它们。不幸的是,您必须复制并粘贴整个子查询才能使其工作。
Aliases are only usable in the query output. You can't use them in other parts of the query. Unfortunately, you'll have to copy and paste the entire subquery to make it work.
你可以这样做,
Access 与 Sql Server 相比有一些差异。
You can do it like this
Access has some differences compared to Sql Server.
因为Access (ACE/Jet) 不符合SQL-92 标准。
考虑这个例子,它是有效的 SQL-92:
事实上,
x
和y
是ORDER BY
子句中唯一有效的元素,因为所有其他元素超出范围(SELECT
子句中的列序号有效,尽管它们的 use id 已弃用)。然而,Access 对上述语法感到窒息。等效的 Access 语法是这样的:
但是,我从 @Remou 的注释中了解到,
ORDER BY
子句中的子查询在 Access 中无效。Because Access (ACE/Jet) is not compliant with the SQL-92 Standard.
Consider this example, which is valid SQL-92:
In fact,
x
andy
the only valid elements in theORDER BY
clause because all others are out of scope (ordinal numbers of columns in theSELECT
clause are valid though their use id deprecated).However, Access chokes on the above syntax. The equivalent Access syntax is this:
However, I understand from @Remou's comments that a subquery in the
ORDER BY
clause is invalid in Access.尝试使用子查询并在外部查询中对结果进行排序。
(未经测试。)
Try using a subquery and order the results in an outer query.
(Not tested.)
怎么样:
How about:
我建议使用中间查询。
如果您调用该 GroupTotalsByRegion,则可以执行以下操作:
您可能认为创建中间查询是额外的工作(从某种意义上来说,确实如此),但您还会发现许多其他查询将基于 GroupTotalsByRegion 。您希望避免在许多其他查询中重复该逻辑。通过将其保留在一个视图中,您可以提供一种简化的途径来回答许多其他问题。
I suggest using an intermediate query.
If you call that GroupTotalsByRegion, you can then do:
You may think it's extra work to create the intermediate query (and, in a sense, it is), but you will also find that many of your other queries will be based off of GroupTotalsByRegion. You want to avoid repeating that logic in many other queries. By keeping it in one view, you provide a simplified route to answering many other questions.
使用如何:
WITH xx AS
(
选择前 1 个 r.nm_regiao, (选择 COUNT(*)
来自 Dw_Empresa
其中 grupo_produto='1' 且
cod_regiao = d.cod_regiao) 总计
来自 Dw_Empresa d
内连接 tb_regiao r ON r.cod_regiao = d.cod_regiao
) SELECT * FROM xx ORDER BY 总计
How about use:
WITH xx AS
(
SELECT TOP 1 r.nm_regiao, (SELECT COUNT(*)
FROM Dw_Empresa
WHERE grupo_produto='1' AND
cod_regiao = d.cod_regiao) as total
FROM Dw_Empresa d
INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao
) SELECT * FROM xx ORDER BY total