Oracle SELECT 帮助

发布于 2024-08-09 21:50:12 字数 1536 浏览 11 评论 0原文

这是我的 SELECT 查询:

SELECT
    a.id_auto,
    SUM(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km) celkova_trzba
FROM Auta a
INNER JOIN (SELECT
            id_auto,
            (SUM(koniec_pozicania - zaciatok_pozicania)) pozicane_dni, 
            (SUM(najazdene_km)) najazdene_km,
            zaloha 
            FROM Zakaznik GROUP BY id_auto) z
ON z.id_auto = a.id_auto 
INNER JOIN (SELECT
            id_auto,
            poplatok_denny,
            poplatok_km 
            FROM Auta_zaloha) az
ON az.id_auto = a.id_auto 
GROUP BY a.id_auto;

但我收到此错误:

ORA-00979: not a GROUP BY expression

有人知道问题出在哪里吗?我有点困惑。我在使用聚合函数 SUM() 的任何地方都有 GROUP BY 子句。

编辑:

另一件事,当我向查询添加 CASE WHEN 时,它停止工作:

SELECT
    a.id_auto, a.poplatok_denny, a.poplatok_km,  
    CASE WHEN z.zaloha IS NULL THEN
        (pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km)
    ELSE 
        (pozicane_dni * az.poplatok_denny + najazdene_km * az.poplatok_km)
    END
        celkova_trzba
FROM Auta a
INNER JOIN (SELECT
            id_auto,
            (SUM(koniec_pozicania - zaciatok_pozicania)) pozicane_dni, 
            (SUM(najazdene_km)) najazdene_km,
            zaloha 
            FROM Zakaznik GROUP BY id_auto, zaloha) z
ON z.id_auto = a.id_auto 
INNER JOIN (SELECT
            id_auto,
            poplatok_denny,
            poplatok_km 
            FROM Auta_zaloha) az 
ON az.id_auto = a.id_auto 
GROUP BY a.id_auto;

Here is my SELECT query:

SELECT
    a.id_auto,
    SUM(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km) celkova_trzba
FROM Auta a
INNER JOIN (SELECT
            id_auto,
            (SUM(koniec_pozicania - zaciatok_pozicania)) pozicane_dni, 
            (SUM(najazdene_km)) najazdene_km,
            zaloha 
            FROM Zakaznik GROUP BY id_auto) z
ON z.id_auto = a.id_auto 
INNER JOIN (SELECT
            id_auto,
            poplatok_denny,
            poplatok_km 
            FROM Auta_zaloha) az
ON az.id_auto = a.id_auto 
GROUP BY a.id_auto;

But I'm getting this error:

ORA-00979: not a GROUP BY expression

Anybody knows where could be the problem? I'm a little bit confused. I have GROUP BY clause everywhere where I use aggregate function SUM().

EDIT:

One more thing, it stop working when I add a CASE WHEN to the query:

SELECT
    a.id_auto, a.poplatok_denny, a.poplatok_km,  
    CASE WHEN z.zaloha IS NULL THEN
        (pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km)
    ELSE 
        (pozicane_dni * az.poplatok_denny + najazdene_km * az.poplatok_km)
    END
        celkova_trzba
FROM Auta a
INNER JOIN (SELECT
            id_auto,
            (SUM(koniec_pozicania - zaciatok_pozicania)) pozicane_dni, 
            (SUM(najazdene_km)) najazdene_km,
            zaloha 
            FROM Zakaznik GROUP BY id_auto, zaloha) z
ON z.id_auto = a.id_auto 
INNER JOIN (SELECT
            id_auto,
            poplatok_denny,
            poplatok_km 
            FROM Auta_zaloha) az 
ON az.id_auto = a.id_auto 
GROUP BY a.id_auto;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

未蓝澄海的烟 2024-08-16 21:50:13

在第一个内部 SELECT 中,您应该删除“zaloha”,对其进行 GROUP BY 或对其应用一些聚合函数。

In your first inner SELECT, you should either remove "zaloha", GROUP BY it or apply some aggregate function to it.

羁客 2024-08-16 21:50:13

问题将出在使用

a.poplatok_km

If you're grouping by a.id_auto 的值,您需要给出一条规则,说明如果结果集中有多行(即使只有一个可能的行,即SQL 不“知道”的东西)。

解决这个问题的方法有两种:

  • 添加表 a 中 group by 子句中计算所需的所有列,
  • 使用像 min (a.poplatok_km) 这样的“伪函数”,它不会改变任何东西

The problem will be in using the value of

a.poplatok_km

If you're grouping by a.id_auto, you need to give a rule on what to do if there are multiple rows in the result set (even if there is only one possible row, that is something the SQL doesn't "know").

Two ways around this:

  • add all columns from table a that are required in the calculation in the group by clause
  • use a "pseudo-Function" like min (a.poplatok_km) which doesn't change anything
花期渐远 2024-08-16 21:50:13

您的第二个内部选择:

INNER JOIN (SELECT
        id_auto,
        poplatok_denny,
        poplatok_km 
        FROM Auta_zaloha) az

似乎缺少 group by 子句,例如:

INNER JOIN (SELECT
        id_auto,
        poplatok_denny,
        poplatok_km 
        FROM Auta_zaloha group by id_auto) az

Your second inner select:

INNER JOIN (SELECT
        id_auto,
        poplatok_denny,
        poplatok_km 
        FROM Auta_zaloha) az

Appears to be missing a group by clause like:

INNER JOIN (SELECT
        id_auto,
        poplatok_denny,
        poplatok_km 
        FROM Auta_zaloha group by id_auto) az
赠意 2024-08-16 21:50:13

尝试使用 NVL2 函数代替 CASE WHEN,至少它可以产生更容易理解的错误消息:

    NVL2(z.zaloha,
(pozicane_dni * az.poplatok_denny + najazdene_km * az.poplatok_km),
(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km)
) celkova_trzba

Try using NVL2 function in place of CASE WHEN, at least it could yield more understandable error message:

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