Re: two case in one select statement

Re: two case in one select statement

 

  

First of all: You make an syntactical error using of ( and ).
Syntactically correct would be like that:

select
( case
when col_1 = 1 then
(v_col_2 - v_col_3)
when col_1 != 1 then
(v_col_2 - v_col_3) * v_col_4
end case,
case
when col_1 = 1 then
v_col_5
when col_1!= 1 then
(v_col_5 * v_col_6)
end case from table1 )


Another:

What a type is x? You said x in sss - sss is sub query, that return 2
values.

Aliases you can define like that:

( case
when T.col_1 = 1 then
(T.v_col_2 - T.v_col_3)
when T.col_1 != 1 then
(T.v_col_2 - T.v_col_3) * T.v_col_4
end case C1,
case
when T.col_1 = 1 then
T.v_col_5
when T.col_1!= 1 then
(T.v_col_5 * T.v_col_6)
end case C2 from table1 T )

That way you define alias for table (T), use it to explicitly define
columns of that table (T.col_1 ...),
and set alias for resulting columns (C1 and C2)

Firman Afandi wrote:

>Hello LazyDBA,
>
>I want to write this script in plsql (oracle 9i):
>---------------------------------------------------
>table1:
>v_col_1
>v_col_2
>v_col_3
>v_col_4
>v_col_5
>v_col_6
>
>
>the script:
>declare
>....
>begin
> BEGIN
> for x in ( select
> case
> when col_1 = 1 then
> (v_col_2 - v_col_3)
> when col_1 != 1 then
> (v_col_2 - v_col_3) * v_col_4)
> end case,
> case
> when col_1 = 1 then
> v_col_5
> when col_1!= 1 then
> (v_col_5 * v_col_6)
> end case from table1 ) loop
> begin
> ...
> end;
> end loop;
>
>end;
>/
>
>and it always return PLS-00402: alias required in SELECT list of cursor to avoid duplicate column names
>
>how to make aliasing of this select statement?
>
>thank's in advanced.
>
>
>
>
>

Oracle LazyDBA home page