Search code examples
sqloracleora-00904

What's wrong with this SQL query?


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

Solution

  • 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