Oracle combines multiple query results in the form of Columns

Source: Internet
Author: User

-- Problem: query the average salary of each department's position

And display it in a query

 

Idea: first, we will find out the three tables, assuming a B c, and then combine a B with each other.

There is no relationship between them, so to connect them, we need to use rownum to use the full outer join and rownum to make table a B in the form of fields (rather than rows) in this way, data in different tables can be merged in the form of columns (the reason is that the union all minus intersect combines multiple query results in the form of rows ). to Merge multiple query results in the form of columns, you need to use rownum and (completely) to solve this problem, the core is rownum and full outer connection.

Code:

Select e3. average department salary, e3. average position salary, e44. average department position salary from

(Select e11. average department salary, e22. average job salary, rownum rn from

(Select e1. average department salary, rownum rn from

(Select avg (sal) average salary from

Emp group bydeptno) e1) e11

Full join

(Select e2. average job salary, rownum rn from

(Select avg (sal) average job salary from

Emp group by job) e2) e22

On e11.rn = e22.rn) e3

Full join

(Select e4. *, rownum rn from

(Select avg (sal) average job salary from

Emp group by deptno, job) e4) e44

On e3.rn = e44.rn;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.