开发者

Why won't Oracle SQL let us use aliases in conditions?

开发者 https://www.devze.com 2022-12-20 05:01 出处:网络
Oracle PL/SQL won\'t let users to us开发者_运维问答e aliases in conditions. In most cases it\'s very practical for me to use aliases instead of long statements. What\'s the reason for that? What bad w

Oracle PL/SQL won't let users to us开发者_运维问答e aliases in conditions. In most cases it's very practical for me to use aliases instead of long statements. What's the reason for that? What bad would happen if we could use aliases in conditions?

Example case: What's wrong with this SQL query?


I think it's just because that is what the SQL standard specifies. I don't agree with gd047 that the alias couldn't be used: the HAVING clause operates on the (intermediate) results of the query i.e. after aggregation, so it would seem pretty straight-forward for the query parser to use the alias to access that result.

You can of course avoid repeating the SUM like this (using the example from the linked question):

SELECT *
from
( SELECT donem, bolge_adi, sehir_tasra "1=S, 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
)
WHERE satis > 0
ORDER BY donem, bolge_adi, sehir_tasra


Column aliases work only with order by clause, because order by is performed after select and all others before select, so they do not have any idea about alias.

You can see this page. I believe that Oracle's engine works the same way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号