如何按采购总额顺序列出每个部门的前十名员工以及每个部门员工应按采购总额排序的顺序?
这些是表格:
CREATE TABLE EMP (
EMPNO NUMBER(4),
ENAME VARCHAR2(20),
POSITION VARCHAR2(20),
DEPTNO NUMBER(2)
);
CREATE TABLE DEPT (
DEPTNO NUMBER(2),
DNAME VARCHAR2(20)
);
CREATE TABLE CLIENT (
CLIENTNO NUMBER(5),
CNAME VARCHAR2(20),
PHONE VARCHAR2(10)
);
CREATE TABLE PURCHASE (
PURCHASENO NUMBER(5),
RECEIPTNO NUMBER(6),
SERVICETYPE VARCHAR2(25),
PAYMENTTYPE VARCHAR2(10),
GST VARCHAR2(3),
AMOUNT NUMBER(4),
SERVEDBY NUMBER(4),
CLIENTNO NUMBER(5)
);
尝试编辑:
SELECT DISTINCT *
FROM (SELECT E.ENAME, D.DEPTNO, COUNT(P.PURCHASENO) AS TOTALAMOUNT,
AVG(P.AMOUNT)AS AVGAMOUNT, MAX(P.AMOUNT)AS HIGHESTAMOUNT,
SUM(P.AMOUNT)AS TOTALAMOUNTPURCHASE,
DENSE_RANK() OVER(PARTITION BY DEPTNO ORDER BY AMOUNT DESC)RANK
FROM EMP E, DEPT D, PURCHASE P
)
WHERE RANK<=10