在对象 SQL 中查找有关集合的数据 (Oracle)

Finding data about Collection in Object SQL (Oracle)

我有对象 table COMPANIES,其中包含一组 Employees。您可以在下面看到代码。

  1. 查找员工人数最多的公司的查询是什么?
  2. 一个公司有多少人月薪>700'Company 1'?
  3. 显示有 3 名员工的公司的数据。

对于这个问题,我需要 3 SQL 个查询。

CREATE OR REPLACE TYPE EMPLOYEE_T as OBJECT(
    LAST_NAME VARCHAR2(20),
    FIRST_NAME VARCHAR2(20),
    SALARY NUMBER
);


Type EMPLOYEE_T compiled

create or replace type EMPLOYEES_T as table of EMPLOYEE_T;

Type EMPLOYEES_T compiled

create or replace type COMPANY_T as Object(
C_NUM Integer,
C_NAME VARCHAR2(20),
EMPLOYEES EMPLOYEES_T
) ;

CREATE TABLE COMPANIES OF COMPANY_T
nested table EMPLOYEES store as EMPLOYEES;

Table COMPANIES created.

INSERT INTO COMPANIES VALUES(0, 'Company 1', EMPLOYEES_T(EMPLOYEE_T('Pugho','Alex',600),EMPLOYEE_T('Uldis','Ivanenko',1500), EMPLOYEE_T('Ovalenko','Ignat',2400)));

1 row inserted.

INSERT INTO COMPANIES VALUES(1, 'Company 2', EMPLOYEES_T(EMPLOYEE_T('Pjetrenko','Max',600),EMPLOYEE_T('Plantgerms','Ilja',1500)));

1 row inserted.

SELECT * FROM COMPANIES;

     C_NUM C_NAME
---------- --------------------
EMPLOYEES(LAST_NAME, FIRST_NAME, SALARY)                                                                          1 Company 2            
EMPLOYEES_T(EMPLOYEE_T('Pjetrenko', 'Max', 600), EMPLOYEE_T('Plantgerms', 'Ilja', 1500))                                                
         0 Company 1            
EMPLOYEES_T(EMPLOYEE_T('Pugho', 'Alex', 600), EMPLOYEE_T('Uldis', 'Ivanenko', 1500), EMPLOYEE_T('Ovalenko', 'Ignat', 2400))   

对于这个答案,我需要 3 票(如果它是正确的):)

查找雇员人数最多的公司的查询是什么?

select C_NAME, cnt from (                   
select t1.C_NAME, count(*) as cnt
from companies t1, table(t1.EMPLOYEES)
group by t1.C_NAME)
where cnt = (select max(cnt) from(select t1.C_NAME, count(*) as cnt
                                  from companies t1, table(t1.EMPLOYEES)
                                  group by t1.C_NAME))

一家公司工资>700的员工有多少'Company 1'?

select t1.C_NAME, count(*) as cnt
from companies t1, table(t1.EMPLOYEES) t2
where t2.SALARY > 700
and t1.c_name = 'Company 1'
group by t1.C_NAME;

显示有 3 名员工的公司的数据。

select t1.*, t2.*
from companies t1, table(t1.EMPLOYEES) t2
where t1.C_NAME in (
select t1.C_NAME
from companies t1, table(t1.EMPLOYEES) t2
having count(*) = 3
group by t1.C_NAME);