對於銷售量最大的產品的pid和名稱兩者均:
從甲骨文12C上有一個fetch
功能,將輸出限制在一個行:
SELECT pu.pid, pr.name, sum(pu.qty) as "total quantity sold"
FROM PURCHASES pu
INNER JOIN PRODUCTS pr on pu.pid = pr.pid
GROUP BY pu.pid, pr.name
ORDER BY sum(pu.qty) DESC
FETCH FIRST 1 ROWS ONLY
;
對於早期版本的Oracle支持row_number() over()
(或具有相同的功能支持其它數據庫):
SELECT pid, name, "total quantity sold"
FROM (
SELECT pu.pid, pr.name, sum(pu.qty) as "total quantity sold"
, ROW_NUMBER() OVER(ORDER BY sum(pu.qty) DESC) AS RN
FROM PURCHASES pu
INNER JOIN PRODUCTS pr on pu.pid = pr.pid
GROUP BY pu.pid, pr.name
)
WHERE RN = 1
;
但是,這兩種方法都假定只有一行要報告。在一組更大的數據中,可能有多行共享最高數量的總和。要返回所有這些行使用dense_rank() over()
代替E,G,:
SELECT pid, name, "total quantity sold"
FROM (
SELECT pu.pid, pr.name, sum(pu.qty) as "total quantity sold"
, DENSE_RANK() OVER(ORDER BY sum(pu.qty) DESC) AS RNK
FROM PURCHASES pu
INNER JOIN PRODUCTS pr on pu.pid = pr.pid
GROUP BY pu.pid, pr.name
)
WHERE RNK = 1
;
注意,樣品中只要有PID的P004/P007/P008是從產品表中缺少。我認爲這只是對這個問題的遺漏。
SQL Fiddle
的Oracle 11g R2架構設置:
CREATE TABLE PURCHASES
(PURno int, EID varchar2(3), PID varchar2(4), CID varchar2(4), QTY int, PTIME timestamp, TOTAL_PRICE numeric)
;
INSERT ALL
INTO PURCHASES (PURno, EID, PID, CID, QTY, PTIME, TOTAL_PRICE)
VALUES (100001, 'e01', 'p002', 'c001', 1, '12-Aug-2017 12:00:00 AM', 211.65)
INTO PURCHASES (PURno, EID, PID, CID, QTY, PTIME, TOTAL_PRICE)
VALUES (100002, 'e01', 'p003', 'c001', 1, '20-Sep-2017 12:00:00 AM', 118.4)
INTO PURCHASES (PURno, EID, PID, CID, QTY, PTIME, TOTAL_PRICE)
VALUES (100003, 'e02', 'p004', 'c002', 5, '08-Oct-2017 12:00:00 AM', 4.95)
INTO PURCHASES (PURno, EID, PID, CID, QTY, PTIME, TOTAL_PRICE)
VALUES (100005, 'e04', 'p007', 'c004', 1, '15-Oct-2017 12:00:00 AM', 119.2)
INTO PURCHASES (PURno, EID, PID, CID, QTY, PTIME, TOTAL_PRICE)
VALUES (100006, 'e03', 'p008', 'c001', 1, '12-Oct-2017 12:00:00 AM', 349.3)
SELECT * FROM dual
;
CREATE TABLE PRODUCTS
(PID varchar2(5), NAME varchar2(70), ORIGINAL_PRICE int, DISCNT_RATE numeric)
;
INSERT ALL
INTO PRODUCTS (PID, NAME, ORIGINAL_PRICE, DISCNT_RATE)
VALUES ('p001', 'stapler', 9.99, .1)
INTO PRODUCTS (PID, NAME, ORIGINAL_PRICE, DISCNT_RATE)
VALUES ('p002', 'TV', 249, .15)
INTO PRODUCTS (PID, NAME, ORIGINAL_PRICE, DISCNT_RATE)
VALUES ('p003', 'camera', 148, .2)
INTO PRODUCTS (PID, NAME, ORIGINAL_PRICE, DISCNT_RATE)
VALUES ('p004', 'p004 missing from sample', 0, 0)
INTO PRODUCTS (PID, NAME, ORIGINAL_PRICE, DISCNT_RATE)
VALUES ('p007', 'p007 missing from sample', 0, 0)
INTO PRODUCTS (PID, NAME, ORIGINAL_PRICE, DISCNT_RATE)
VALUES ('p008', 'p008 missing from sample', 0, 0)
SELECT * FROM dual
;
查詢1:
SELECT PID, NAME, "total quantity sold"
FROM (
SELECT pu.pid, pr.name, sum(pu.qty) as "total quantity sold"
, ROW_NUMBER() OVER(ORDER BY sum(pu.qty) DESC) AS RN
FROM PURCHASES pu
INNER JOIN PRODUCTS pr on pu.pid = pr.pid
GROUP BY pu.pid, pr.name
)
WHERE RN = 1
;
Results:
| PID | NAME | total quantity sold |
|------|--------------------------|---------------------|
| p004 | p004 missing from sample | 5 |
查詢2:
SELECT PID, NAME, "total quantity sold"
FROM (
SELECT pu.pid, pr.name, sum(pu.qty) as "total quantity sold"
, DENSE_RANK() OVER(ORDER BY sum(pu.qty) DESC) AS RNK
FROM PURCHASES pu
INNER JOIN PRODUCTS pr on pu.pid = pr.pid
GROUP BY pu.pid, pr.name
)
WHERE RNK = 1
;
Results:
| PID | NAME | total quantity sold |
|------|--------------------------|---------------------|
| p004 | p004 missing from sample | 5 |
你能分享你正在使用生成數據的SQL? –