How to Have Function Definition and Subquery in With Clause of Oracle Select Statement?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to Have Function Definition and Subquery in With Clause of Oracle Select Statement?



I know the right syntax for having a function definition in the WITH clause. I know the right syntax for having a subquery in the WITH clause. But I have been unable to find an example of having a subquery and a function definition in the WITH clause of a SELECT statement.


WITH


WITH


WITH


SELECT



If I have:


with totals as ( select colum_name from some_table )
select sum(column_name) from totals;



How do I add a function definition in the WITH clause?


WITH





What happened when you tried to do that? Did you get any errors? Let us know what is that you were trying to accomplish with some sample data and expected results.
– Kaushik Nayak
Aug 8 at 4:03




1 Answer
1



I don't think there's any such restriction. However, I suspect that your problem has to do with column aliasing. Here's what worked for me:


with totals as (select sum(column_name) c1 from some_table)
select c1 from totals;



Oracle might have complained because you were trying to do something like:


with totals as (select sum(column_name) from some_table)
select sum(column_name) from totals;



Unfortunately, this is a consequence of name resolution. The subquery's column will get named "sum(column_name)". Since sum is a function, there's no way to reference that column name without Oracle thinking you're referencing the function. You have to give it another name in order to reference it anywhere else.



Edit: It seems that you want to define a function as if you would a view subquery. I don't think anything like this is possible. View subqueries really only perform textual substitution.



PL/SQL functions require a whole different parser, name resolution, compilation process, etc. Having them work in queries alone is hard enough.



Sorry to say, but you'd have to define your packages/procedures/functions normally.





Thank you for the answer, but it seems that I was not clear in what I posted originally. I updated my question to clarify that I meant "function definition" and not simply using a function.
– kurzweil4
Aug 8 at 4:06





@kurzweil4 I think I got what you mean. Sorry to say, it's not possible.
– Jonathan Park
Aug 8 at 5:03






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard