MS-Access->选择为 +排序依据 = 错误

发布于 2024-09-28 11:52:57 字数 431 浏览 5 评论 0原文

我正在尝试进行查询以检索甜品销量最高的地区。 “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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

毅然前行 2024-10-05 11:52:57

我知道老问题,但它可能会帮助某人了解,虽然你不能按别名排序,但你可以按列索引排序。例如,这将不会出现错误:

SELECT 
 firstColumn,
 IIF(secondColumn = '', thirdColumn, secondColumn) As yourAlias
FROM
 yourTable
ORDER BY
 2 ASC

然后,结果将按第二列中找到的值(别名“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 :

SELECT 
 firstColumn,
 IIF(secondColumn = '', thirdColumn, secondColumn) As yourAlias
FROM
 yourTable
ORDER BY
 2 ASC

The results would then be ordered by the values found in the second column wich is the Alias "yourAlias".

旧人 2024-10-05 11:52:57

别名只能在查询输出中使用。您不能在查询的其他部分使用它们。不幸的是,您必须复制并粘贴整个子查询才能使其工作。

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.

败给现实 2024-10-05 11:52:57

你可以这样做,

select * from(
  select a + b as c, * from table)
  order by c

Access 与 Sql Server 相比有一些差异。

You can do it like this

select * from(
  select a + b as c, * from table)
  order by c

Access has some differences compared to Sql Server.

一身骄傲 2024-10-05 11:52:57

为什么它不考虑新的
创建了我在选择中创建的“列”
条款?

因为Access (ACE/Jet) 不符合SQL-92 标准。

考虑这个例子,它是有效的 SQL-92:

SELECT a AS x, c - b AS y
  FROM MyTable
 ORDER
    BY x, y;

事实上,xyORDER BY 子句中唯一有效的元素,因为所有其他元素超出范围(SELECT 子句中的列序号有效,尽管它们的 use id 已弃用)。

然而,Access 对上述语法感到窒息。等效的 Access 语法是这样的:

SELECT a AS x, c - b AS y
  FROM MyTable
 ORDER
    BY a, c - b;

但是,我从 @Remou 的注释中了解到,ORDER BY 子句中的子查询在 Access 中无效。

Why it doesn't consider the newly
created 'column' I made in the select
clause?

Because Access (ACE/Jet) is not compliant with the SQL-92 Standard.

Consider this example, which is valid SQL-92:

SELECT a AS x, c - b AS y
  FROM MyTable
 ORDER
    BY x, y;

In fact, x and y the only valid elements in the ORDER BY clause because all others are out of scope (ordinal numbers of columns in the SELECT clause are valid though their use id deprecated).

However, Access chokes on the above syntax. The equivalent Access syntax is this:

SELECT a AS x, c - b AS y
  FROM MyTable
 ORDER
    BY a, c - b;

However, I understand from @Remou's comments that a subquery in the ORDER BY clause is invalid in Access.

要走就滚别墨迹 2024-10-05 11:52:57

尝试使用子查询并在外部查询中对结果进行排序。

SELECT TOP 1 * FROM
(
    SELECT
        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
) T1
ORDER BY total DESC

(未经测试。)

Try using a subquery and order the results in an outer query.

SELECT TOP 1 * FROM
(
    SELECT
        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
) T1
ORDER BY total DESC

(Not tested.)

惜醉颜 2024-10-05 11:52:57

怎么样:

SELECT TOP 1  r.nm_regiao 
FROM (SELECT Dw_Empresa.cod_regiao, 
             Count(Dw_Empresa.cod_regiao) AS CountOfcod_regiao
      FROM Dw_Empresa
      WHERE Dw_Empresa.[grupo_produto]='1'
      GROUP BY Dw_Empresa.cod_regiao
      ORDER BY Count(Dw_Empresa.cod_regiao) DESC) d
INNER JOIN tb_regiao AS r 
ON d.cod_regiao = r.cod_regiao

How about:

SELECT TOP 1  r.nm_regiao 
FROM (SELECT Dw_Empresa.cod_regiao, 
             Count(Dw_Empresa.cod_regiao) AS CountOfcod_regiao
      FROM Dw_Empresa
      WHERE Dw_Empresa.[grupo_produto]='1'
      GROUP BY Dw_Empresa.cod_regiao
      ORDER BY Count(Dw_Empresa.cod_regiao) DESC) d
INNER JOIN tb_regiao AS r 
ON d.cod_regiao = r.cod_regiao
太阳男子 2024-10-05 11:52:57

我建议使用中间查询。

 SELECT r.nm_regiao, d.grupo_produto, COUNT(*) AS total
   FROM Dw_Empresa d INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao
   GROUP BY r.nm_regiao, d.grupo_produto;

如果您调用该 GroupTotalsByRegion,则可以执行以下操作:

SELECT TOP 1 nm_regiao, total FROM GroupTotalsByRegion 
  WHERE grupo_produto = '1' ORDER BY total DESC

您可能认为创建中间查询是额外的工作(从某种意义上来说,确实如此),但您还会发现许多其他查询将基于 GroupTotalsByRegion 。您希望避免在许多其他查询中重复该逻辑。通过将其保留在一个视图中,您可以提供一种简化的途径来回答许多其他问题。

I suggest using an intermediate query.

 SELECT r.nm_regiao, d.grupo_produto, COUNT(*) AS total
   FROM Dw_Empresa d INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao
   GROUP BY r.nm_regiao, d.grupo_produto;

If you call that GroupTotalsByRegion, you can then do:

SELECT TOP 1 nm_regiao, total FROM GroupTotalsByRegion 
  WHERE grupo_produto = '1' ORDER BY total DESC

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.

ゃ懵逼小萝莉 2024-10-05 11:52:57

使用如何:
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文