What's wrong with this SQL query?

我与影子孤独终老i 提交于 2019-12-11 12:37:21

问题


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

回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2235166/whats-wrong-with-this-sql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!