Just learned something new today - can include a column complete zero in a UNION statement

I just thought it was pretty cool.

SELECT null, job_id, NULL as manager_id, AVG (salary) as AVGSAL

Employees

GROUP BY department_id, job_id

UNION ALL

SELECT NULL, null, null, avg (salary) as AVGSAL

OF hr.employees

GROUP BY job_id, manager_id;

I watched this and me "... sounds weird, how are the corresponding data types up."

then I tried to run some scripts of myself and it works great. I guess that since the "datatype" NULL is not who collide with other data types, it makes sense.

Huh? Why wouldn't you be able to include a null full column in a set of query results?

Why do you think that it does not work?

SELECT null, job_id, NULL as manager_id, AVG (salary) as AVGSAL

Employees

GROUP BY department_id, job_id

UNION ALL

SELECT NULL, null, null, avg (salary) as AVGSAL

OF hr.employees

GROUP BY job_id, manager_id;

I watched this and me "... sounds weird, how are the corresponding data types up."

then I tried to run some scripts of myself and it works great. I guess that since the "datatype" NULL is not who collide with other data types, it makes sense.

No - there is NO SUCH THING as a NULL data type. A null is NOT a type of data: it represents an UNKNOWN value and does NOT have a data type.

Because NULL is not a dataype is NOT in conflict with ANY data type of a column to a different query.

See the sections of Oracle doc on NULL values

http://docs.Oracle.com/CD/B28359_01/server.111/b28286/sql_elements005.htm

You can convert a NULL value to a data type specific well and then because you have a conflict of data type, you will get an exception:

SELECT cast (null as varchar2 (10)), job_id, cast (NULL as number (10)) as manager_id,.

AVG (salary) as AVGSAL FROM employees

GROUP BY department_id, job_id

UNION ALL

SELECT cast (NULL as number (10)), null, cast (null as varchar2 (10)).

AVG (salary) as hr.employees AVGSAL

GROUP BY job_id, manager_id;

ORA-01790: expression must have same type of data, matching expression

This exception is due to the incompatibility of data type of explicit CASTs.

See the CAST operator in the Oracle documentation

http://docs.Oracle.com/CD/B28359_01/server.111/b28286/functions016.htm

CASTConverts a type of built-in data or value typed collection to another type of built-in data or value-typed collection.

Tags: Database

Similar Questions

Maybe you are looking for