Use Cartesian Product and external join to retain row data

Source: Internet
Author: User
Create Table Company (ID number (4), company varchar2 (20); -- create a new unit table.

Create Table Factory (ID number (4), factory varchar2 (20); -- create a new manufacturer

Create Table SPEC (ID number (4), spec varchar2 (20); -- create a new type and model table.

SQL> Create Table equip (ID number (4), equip varchar (20), companyid number (4), factoryid number (4), specd
ID number (4); -- creates a device table.

Insert into company values (1, 'unit: 1 ');

Insert into company values (2, 'unit: 2 ');

Insert into company values (3, 'unit: 3 ');

Insert into factory values (1, 'factory 1 ');

Insert into factory values (2, 'factory 2 ');

Insert into factory values (3, 'factory 3 ');

Insert into factory values (4, 'factory 4 ');

Insert into Spec values (1, 'Type 1 ');

Insert into Spec values (2, 'Type and model 2 ');

Insert into Spec values (3, 'Type and model 3 ');

Insert into Spec values (4, 'Type 4 ');

Insert into equip (ID, equip, companyid, factoryid, specdid) values (1, 'device name _ 1', 1, 2, 3 );

The query statement is as follows:
SQL> select a. Company "unit name ",
2 A. Factory "Manufacturer no ",
3 A. spec "type and model ",
4 count (B. ID) "device quantity"
5 from (select C. ID c_id,
6 C. Company,
7 F. ID f_id,
8 F. factory,
9 s. ID s_id,
10 s. spec
11 from Company C, factory F, spec s) A, equip B
12 where a. c_id = B. companyid (+)
13 and A. f_id = B. factoryid (+)
14 and A. s_id = B. specdid (+)
15 group by A. Company, A. Factory, A. spec;

Organization Name Manufacturer no., specification, and model number
----------------------------------------------------------------------
Unit 1 manufacturer 1 specification model 1 0
Unit 1 manufacturer 1 specification model 2 0
Unit 1 manufacturer 1 specification model 3 0
Unit 1 manufacturer 1 specification model 4 0
Unit 1 manufacturer 2 specification model 1 0
Unit 1 manufacturer 2 specification model 2 0
Unit 1 manufacturer 2 specification model 3 1
Unit 1 manufacturer 2 specification model 4 0
Unit 1 manufacturer 3 specification model 1 0
Unit 1 manufacturer 3 specification model 2 0
Unit 1 manufacturer 3 specification model 3 0
Unit 1 manufacturer 3 specification model 4 0
Unit 1 manufacturer 4 specification model 1 0
Unit 1 manufacturer 4 specification model 2 0
Unit 1 manufacturer 4 specification model 3 0
Unit 1 manufacturer 4 specification model 4 0
Unit 2 manufacturers 1 specification model 1 0
Unit 2 manufacturers 1 specification model 2 0
Unit 2 manufacturers 1 specification model 3 0
Unit 2 manufacturers 1 specification model 4 0

Organization Name Manufacturer no., specification, and model number
----------------------------------------------------------------------
Unit 2 manufacturer 2 specification model 1 0
Unit 2 manufacturer 2 specification model 2 0
Unit 2 manufacturer 2 specification model 3 0
Unit 2 manufacturer 2 specification model 4 0
Unit 2 manufacturers 3 specification model 1 0
Unit 2 manufacturer 3 specification model 2 0
Unit 2 manufacturer 3 specification model 3 0
Unit 2 manufacturer 3 specification model 4 0
Unit 2 manufacturer 4 specification model 1 0
Unit 2 manufacturer 4 specification model 2 0
Unit 2 manufacturer 4 specification model 3 0
Unit 2 manufacturer 4 specification model 4 0
Unit 3 manufacturer 1 specification model 1 0
Unit 3 manufacturer 1 specification model 2 0
Unit 3 manufacturer 1 specification model 3 0
Unit 3 manufacturer 1 specification model 4 0
Unit 3 manufacturer 2 specification model 1 0
Unit 3 manufacturer 2 specification model 2 0
Unit 3 manufacturer 2 specification model 3 0
Unit 3 manufacturer 2 specification model 4 0
Unit 3 manufacturer 3 specification model 1 0

Organization Name Manufacturer no., specification, and model number
----------------------------------------------------------------------
Unit 3 manufacturer 3 specification model 2 0
Unit 3 manufacturer 3 specification model 3 0
Unit 3 manufacturer 3 specification model 4 0
Unit 3 manufacturer 4 specification model 1 0
Unit 3 manufacturer 4 specification model 2 0
Unit 3 manufacturer 4 specification model 3 0
Unit 3 manufacturer 4 specification model 4 0

48 rows selected

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.