我无法计算 SQL 代码中的除法

发布于 2024-08-19 15:31:02 字数 1305 浏览 5 评论 0原文

以下代码工作没有问题:

select donem, mekankodu, count(yayin_kodu) yc, 
       SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) yys
from 
(    select donem,
            bayi_adi,
            bayi_kodu,
            x.mekankodu,
            mekan_adi,
            mekan_tipi,
            yayin_kodu,
            yayin_adi,
            sum(x.b2b_dagitim + x.b2b_transfer) sevk,
            sum(x.b2b_iade) iade,
            sum(x.b2b_dagitim + x.b2b_transfer - x.b2b_iade) satis
    from mps_view2 x
    where x.donem = '200910'
    and x.yayin_kodu in (93,95,98,99,103,174,307,309,311,489,491,495,533,534,538,605,641,642,650,2539,
                         2697,4560,5049,5772,5950,5980,7318,7440,8086,8524,11161,12707,12708,12709,14376,
                         15107,15309,15633)
    and x.mekankodu in (31851,38569,7123,7403,7481)
    group by donem, bayi_adi, bayi_kodu, mekankodu, mekan_adi, mekan_tipi, yayin_kodu, yayin_adi
    order by donem, bayi_kodu, mekan_adi, yayin_adi
)
group by donem, mekankodu

当我想在结果表中包含 yc/yss 时,我按如下方式排列 SELECT 部分:

select donem, mekankodu, count(yayin_kodu) yc, 
       SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) yys,
       yc/yys yo

但它给出

ORA-00904 错误:YYS:无效 标识符。

我应该怎么办?

The following code works without problems:

select donem, mekankodu, count(yayin_kodu) yc, 
       SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) yys
from 
(    select donem,
            bayi_adi,
            bayi_kodu,
            x.mekankodu,
            mekan_adi,
            mekan_tipi,
            yayin_kodu,
            yayin_adi,
            sum(x.b2b_dagitim + x.b2b_transfer) sevk,
            sum(x.b2b_iade) iade,
            sum(x.b2b_dagitim + x.b2b_transfer - x.b2b_iade) satis
    from mps_view2 x
    where x.donem = '200910'
    and x.yayin_kodu in (93,95,98,99,103,174,307,309,311,489,491,495,533,534,538,605,641,642,650,2539,
                         2697,4560,5049,5772,5950,5980,7318,7440,8086,8524,11161,12707,12708,12709,14376,
                         15107,15309,15633)
    and x.mekankodu in (31851,38569,7123,7403,7481)
    group by donem, bayi_adi, bayi_kodu, mekankodu, mekan_adi, mekan_tipi, yayin_kodu, yayin_adi
    order by donem, bayi_kodu, mekan_adi, yayin_adi
)
group by donem, mekankodu

When I want to include yc/yss in my result table, I arrange the SELECT part as follows:

select donem, mekankodu, count(yayin_kodu) yc, 
       SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) yys,
       yc/yys yo

but it gives

ORA-00904 Error: YYS: Invalid
identifier.

What should I do?

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

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

发布评论

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

评论(4

银河中√捞星星 2024-08-26 15:31:02

使用with语句:

with subquery_name as
(
    select
       donem,
       mekankodu,
       count(yayin_kodu) as yc, 
       SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) as yys
    from
        ...
)
select
    donem,
    mekankodu,
    yc,
    yys,
    yc/yys as yo
from
    subquery_name

这是PL/SQL的一个非常有用的功能。您试图访问同一查询中生成的列,这是不可能的。 with 语句将允许您根据另一个计算列计算新列,就像您在此处所做的那样。

Use a with statement:

with subquery_name as
(
    select
       donem,
       mekankodu,
       count(yayin_kodu) as yc, 
       SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) as yys
    from
        ...
)
select
    donem,
    mekankodu,
    yc,
    yys,
    yc/yys as yo
from
    subquery_name

This is a very useful feature of PL/SQL. You're trying to access a generated column within the same query, which is not possible. A with statement will allow you to calculate a new column based on another calculated column as you are doing here.

飘落散花 2024-08-26 15:31:02

您不能引用同一选择中的其他列。
使用子选择:

select donem, mekankodu, yc, yys, yc/yys yo
from
(
    select donem, mekankodu, count(yayin_kodu) yc,
        SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) yys,
        yc/yys yo
    from ...
) Sub

You cannot refer to other columns in the same select.
Use a sub-select:

select donem, mekankodu, yc, yys, yc/yys yo
from
(
    select donem, mekankodu, count(yayin_kodu) yc,
        SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) yys,
        yc/yys yo
    from ...
) Sub
请持续率性 2024-08-26 15:31:02

您无法在 SQL 中访问邻居创建的列,但是可以访问数据目录:

select donem, mekankodu, count(yayin_kodu) yc, 
       SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) yys,
       count(yayin_kodu)/SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) "yc/yys"
from 
(    select donem,
            bayi_adi,
            bayi_kodu,
            x.mekankodu,
            mekan_adi,
            mekan_tipi,
            yayin_kodu,
            yayin_adi,
            sum(x.b2b_dagitim + x.b2b_transfer) sevk,
            sum(x.b2b_iade) iade,
            sum(x.b2b_dagitim + x.b2b_transfer - x.b2b_iade) satis
    from mps_view2 x
    where x.donem = '200910'
    and x.yayin_kodu in (93,95,98,99,103,174,307,309,311,489,491,495,533,534,538,605,641,642,650,2539,
                         2697,4560,5049,5772,5950,5980,7318,7440,8086,8524,11161,12707,12708,12709,14376,
                         15107,15309,15633)
    and x.mekankodu in (31851,38569,7123,7403,7481)
    group by donem, bayi_adi, bayi_kodu, mekankodu, mekan_adi, mekan_tipi, yayin_kodu, yayin_adi
    order by donem, bayi_kodu, mekan_adi, yayin_adi
)
group by donem, mekankodu

注意:这并不像看起来那样效率低下...SQL 足够智能,可以在大多数情况下执行一次计算并使用两次结果案例。通常,相信优化器。

You can't access a neighborly-created column in SQL, you can however access the data directory:

select donem, mekankodu, count(yayin_kodu) yc, 
       SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) yys,
       count(yayin_kodu)/SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) "yc/yys"
from 
(    select donem,
            bayi_adi,
            bayi_kodu,
            x.mekankodu,
            mekan_adi,
            mekan_tipi,
            yayin_kodu,
            yayin_adi,
            sum(x.b2b_dagitim + x.b2b_transfer) sevk,
            sum(x.b2b_iade) iade,
            sum(x.b2b_dagitim + x.b2b_transfer - x.b2b_iade) satis
    from mps_view2 x
    where x.donem = '200910'
    and x.yayin_kodu in (93,95,98,99,103,174,307,309,311,489,491,495,533,534,538,605,641,642,650,2539,
                         2697,4560,5049,5772,5950,5980,7318,7440,8086,8524,11161,12707,12708,12709,14376,
                         15107,15309,15633)
    and x.mekankodu in (31851,38569,7123,7403,7481)
    group by donem, bayi_adi, bayi_kodu, mekankodu, mekan_adi, mekan_tipi, yayin_kodu, yayin_adi
    order by donem, bayi_kodu, mekan_adi, yayin_adi
)
group by donem, mekankodu

Note: This isn't inefficient like it looks...SQL is smart enough to do the calculation once and use the result twice in most cases. More often than not, trust the optimizer.

咿呀咿呀哟 2024-08-26 15:31:02

你忘了“如”吗?

select donem, mekankodu, count(yayin_kodu) yc, 
   SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) AS yys 

Did you forget "as"?

select donem, mekankodu, count(yayin_kodu) yc, 
   SUM(CASE WHEN iade =0 THEN 1 ELSE 0 END) AS yys 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文