我无法计算 SQL 代码中的除法
以下代码工作没有问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用with语句:
这是PL/SQL的一个非常有用的功能。您试图访问同一查询中生成的列,这是不可能的。 with 语句将允许您根据另一个计算列计算新列,就像您在此处所做的那样。
Use a with statement:
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.
您不能引用同一选择中的其他列。
使用子选择:
You cannot refer to other columns in the same select.
Use a sub-select:
您无法在 SQL 中访问邻居创建的列,但是可以访问数据目录:
注意:这并不像看起来那样效率低下...SQL 足够智能,可以在大多数情况下执行一次计算并使用两次结果案例。通常,相信优化器。
You can't access a neighborly-created column in SQL, you can however access the data directory:
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.
你忘了“如”吗?
Did you forget "as"?