我有对象表COMPANIES,其中包含员工集合。您可以在下面看到的代码。
对于这个问题,我需要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))
公司“ Company 1”中有多少名员工的工资超过700?
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);