'Oracle 10g - Alias column from query

My column is a result of a subquery, I would like to assign a column alias field is extracted from another query, but I can not.

Carry the example does not work:

SELECT field1, 
       field2,
       (SELECT FROM mytable WHERE COND1 aa) as [SELECT name FROM MyTable WHERE cond2]
  FROM table1

is it possible? thanks



Solution 1:[1]

In some cases (excluding ons mentioned by X-Zero) you can do that with multiple queries and dynamic SQL.

DECLARE 
  v_alias VARCHAR2(30);
  v_query_alias VARCHAR2(1000);
BEGIN
  SELECT name 
  INTO v_alias 
  FROM MyTable 
  WHERE cond2;

  EXECUTE IMMEDIATE 'SELECT field1
                           ,field2
                           ,(SELECT FROM mytable WHERE COND1 aa) as '||v_alias||'
                     FROM table1';
END;
/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jva