这个SQL查询有什么问题?

时间:2010-02-10 07:44:18

标签: sql oracle ora-00904

以下查询返回“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

3 个答案:

答案 0 :(得分:7)

您不能在条件中使用别名(包含查询部分)

试试这个:

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

答案 1 :(得分:4)

从这里开始:

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

  

别名可用于   order_by_clause但不是其他条款   在查询中。

答案 2 :(得分:1)

您需要将其更改为

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子句中使用别名。