这个 SQL 查询有什么问题?

发布于 2024-08-21 12:18:56 字数 477 浏览 7 评论 0原文

以下查询返回“ORA-00904 错误:SATIS:标识符无效。”当我删除行 HAVING satis >; 0,它有效。我应该怎么办?

SELECT donem, bolge_adi, sehir_tasra "1=Ş, 2=T", 
       COUNT(DISTINCT mekankodu) "M.SAYISI",
       SUM(b2b_dagitim + b2b_transfer - b2b_iade) satis
FROM mps_view2
WHERE donem IN ('200612','200712','200812','200912')
AND (ob IS NOT NULL OR b2b_ob IS NOT NULL)
GROUP BY donem, bolge_adi, sehir_tasra
HAVING satis > 0
ORDER BY donem, bolge_adi, sehir_tasra

The following query returns "ORA-00904 error: SATIS: Invalid identifier." When I remove the line HAVING satis > 0, it works. What should I do?

SELECT donem, bolge_adi, sehir_tasra "1=Ş, 2=T", 
       COUNT(DISTINCT mekankodu) "M.SAYISI",
       SUM(b2b_dagitim + b2b_transfer - b2b_iade) satis
FROM mps_view2
WHERE donem IN ('200612','200712','200812','200912')
AND (ob IS NOT NULL OR b2b_ob IS NOT NULL)
GROUP BY donem, bolge_adi, sehir_tasra
HAVING satis > 0
ORDER BY donem, bolge_adi, sehir_tasra

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

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

发布评论

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

评论(3

不一样的天空 2024-08-28 12:18:56

您不能在条件(具有查询部分)中使用别名,

请尝试以下操作:

SELECT donem, bolge_adi, sehir_tasra "1=Ş, 2=T", 
   COUNT(DISTINCT mekankodu) "M.SAYISI",
   SUM(b2b_dagitim + b2b_transfer - b2b_iade) satis
FROM mps_view2
WHERE donem IN ('200612','200712','200812','200912')
      AND (ob IS NOT NULL OR b2b_ob IS NOT NULL)
GROUP BY donem, bolge_adi, sehir_tasra
HAVING SUM(b2b_dagitim + b2b_transfer - b2b_iade) > 0
ORDER BY donem, bolge_adi, sehir_tasra

You can not use alias in conditions (having section of your query)

try this one:

SELECT donem, bolge_adi, sehir_tasra "1=Ş, 2=T", 
   COUNT(DISTINCT mekankodu) "M.SAYISI",
   SUM(b2b_dagitim + b2b_transfer - b2b_iade) satis
FROM mps_view2
WHERE donem IN ('200612','200712','200812','200912')
      AND (ob IS NOT NULL OR b2b_ob IS NOT NULL)
GROUP BY donem, bolge_adi, sehir_tasra
HAVING SUM(b2b_dagitim + b2b_transfer - b2b_iade) > 0
ORDER BY donem, bolge_adi, sehir_tasra
彩虹直至黑白 2024-08-28 12:18:56

从这里:

http://download.oracle.com /docs/cd/B19306_01/server.102/b14200/statements_10002.htm

别名可以用在
order_by_clause 但不是其他子句
在查询中。

From here:

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm

The alias can be used in the
order_by_clause but not other clauses
in the query.

笨死的猪 2024-08-28 12:18:56

您需要将其更改为

SELECT donem, bolge_adi, sehir_tasra "1=Ş, 2=T",  
       COUNT(DISTINCT mekankodu) "M.SAYISI", 
       SUM(b2b_dagitim + b2b_transfer - b2b_iade) satis 
FROM mps_view2 
WHERE donem IN ('200612','200712','200812','200912') 
AND (ob IS NOT NULL OR b2b_ob IS NOT NULL) 
GROUP BY donem, bolge_adi, sehir_tasra 
HAVING SUM(b2b_dagitim + b2b_transfer - b2b_iade) > 0
ORDER BY donem, bolge_adi, sehir_tasra 

您不能在 HAVING 子句中使用别名。

You need to change it to

SELECT donem, bolge_adi, sehir_tasra "1=Ş, 2=T",  
       COUNT(DISTINCT mekankodu) "M.SAYISI", 
       SUM(b2b_dagitim + b2b_transfer - b2b_iade) satis 
FROM mps_view2 
WHERE donem IN ('200612','200712','200812','200912') 
AND (ob IS NOT NULL OR b2b_ob IS NOT NULL) 
GROUP BY donem, bolge_adi, sehir_tasra 
HAVING SUM(b2b_dagitim + b2b_transfer - b2b_iade) > 0
ORDER BY donem, bolge_adi, sehir_tasra 

You cannot use the alias in the HAVING clause.

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