在Oracle SQL

时间:2018-04-14 19:24:13

标签: sql oracle oracle11g

我是SQL的新手,我试图按名称按升序排列所有组织,并且所有员工都属于该组织。我有下面的架构。有人可以提供任何帮助吗?我还试图按名称列出所有员工,如果员工是至少一个组织的经理,请在结果中提供一行名称 他/她管理的每个组织都是null。

SELECT Org.name, MAX(Employee.salary) AS "Highest salary"
From  Org, Employee
GROUP BY Org.name;

以上代码为我提供了所有具有全局最高工资的组织名称的列表,但我正在寻找每个组织的最大值。我想我可能需要使用一些连接,但我不是很熟悉。

CREATE TABLE Employee
(
  employeeId numeric(9) not null,
  name varchar2(100) unique,
  salary numeric(9) not null,
  CONSTRAINT employeeId_pk PRIMARY KEY (employeeId)
);

CREATE TABLE Org
(
  orgId numeric(9) not null,
  name varchar2(100) not null unique,
  managerId numeric(9) not null,
  CONSTRAINT orgId_pk PRIMARY KEY (orgId),
  CONSTRAINT managerId_fk FOREIGN KEY (managerId)
    REFERENCES Employee(employeeId)
);

CREATE TABLE EmployeeOrg
(
  employeeId numeric(9) not null,
  orgId numeric(9) not null,
  CONSTRAINT employeeId_orgId_pk PRIMARY KEY (employeeId, orgId),
  CONSTRAINT employeeId_fk FOREIGN KEY (employeeId)
    REFERENCES Employee(employeeId),
  CONSTRAINT orgId_fk FOREIGN KEY (orgId)
    REFERENCES Org(orgId)
);

INSERT ALL
INTO Employee (employeeId, name, salary) VALUES (123, 'Jim', 123)
INTO Employee (employeeId, name, salary) VALUES (456, 'Bill', 1456)
INTO Employee (employeeId, name, salary) VALUES (789, 'Frank', 456)
INTO Employee (employeeId, name, salary) VALUES (987, 'Sara', 45668)
INTO Employee (employeeId, name, salary) VALUES (654, 'Liz', 4456)
INTO Employee (employeeId, name, salary) VALUES (321, 'Morgan', 4556)
SELECT * FROM dual;

INSERT ALL
INTO Org (orgId, name, managerId) VALUES (1, 'Sales', 123)
INTO Org (orgId, name, managerId) VALUES (2, 'HR', 789)
INTO Org (orgId, name, managerId) VALUES (3, 'E Suite', 987)
INTO Org (orgId, name, managerId) VALUES (4, 'Marketing', 654)
SELECT * FROM dual;

INSERT ALL
INTO EmployeeOrg (employeeId, orgId) VALUES (123, 1)
INTO EmployeeOrg (employeeId, orgId) VALUES (789, 2)
INTO EmployeeOrg (employeeId, orgId) VALUES (987, 3)
INTO EmployeeOrg (employeeId, orgId) VALUES (654, 4)
INTO EmployeeOrg (employeeId, orgId) VALUES (123, 4)
INTO EmployeeOrg (employeeId, orgId) VALUES (456, 1)
INTO EmployeeOrg (employeeId, orgId) VALUES (321, 2)
INTO EmployeeOrg (employeeId, orgId) VALUES (789, 4)
INTO EmployeeOrg (employeeId, orgId) VALUES (456, 2)
SELECT * FROM dual;

编辑(来自OP的评论)

以下代码为我提供了所有具有全局最高工资的组织名称的列表,但我正在寻找每个组织的最大值。我想我可能需要使用一些连接,但我不是很熟悉。

SELECT Org.name, MAX(Employee.salary) AS "Highest salary"
  From Org, Employee
  GROUP BY Org.name

1 个答案:

答案 0 :(得分:1)

model.add_loss()