我需要返回已訂購最大數量項目的客戶列表。我已添加以下內容並獲得一些結果,但需要過濾查詢以顯示多個最大值。DBMS - Oracle |返回多個表/關係的多個最大值
DDL
CREATE TABLE Customer
(Cust_Num NUMBER(4) PRIMARY KEY,
Cust_LName VARCHAR2(10),
Cust_FName VARCHAR2(10),
Cust_Address VARCHAR2(20),
Cust_City VARCHAR2(12),
Cust_State VARCHAR2(2),
Cust_Zip VARCHAR2(5),
Cust_Referred NUMBER(4));
Create Table BookOrder
(BO_OrderNum NUMBER(4) PRIMARY KEY,
BO_CustNum NUMBER(4) REFERENCES Customer(Cust_Num),
BO_OrderDate DATE,
BO_ShipDate DATE,
BO_ShipStreet VARCHAR2(18),
BO_ShipCity VARCHAR2(15),
BO_ShipState VARCHAR2(2),
BO_ShipZip VARCHAR2(5));
CREATE TABLE BookOrderItem
(BOI_OrderNum NUMBER(4) NOT NULL REFERENCES BookOrder(BO_OrderNum),
BOI_ItemNum NUMBER(2) NOT NULL,
BOI_ISBN VARCHAR2(10) REFERENCES Book(Book_ISBN),
BOI_Qty NUMBER(3),
CONSTRAINT bookorderitem_pk PRIMARY KEY (BOI_OrderNum, BOI_ItemNum));
我寫了下面的DML:
SELECT C.CUST_LNAME, C.CUST_FNAME, BO.BO_CUSTNUM, BOI.BOI_ORDERNUM,
COUNT(BOI.BOI_ITEMNUM) AS Total_Items_Per_Order
FROM BookOrderItem BOI JOIN BookOrder BO ON BOI.BOI_OrderNum = BO.BO_OrderNum
JOIN Customer C ON C.Cust_Num = BO.BO_CustNum
GROUP BY C.Cust_LName, C.CUST_FName, BO.BO_CustNum, BOI.BOI_OrderNum
ORDER BY Total_Item_Per_Order DESC;
這給了我下面的結果...
+------------+------------+------------+--------------+-----------------------+
| CUST_LNAME | CUST_FNAME | BO_CustNum | BOI_OrderNum | TOTAL_ITEMS_PER_ORDER |
+------------+------------+------------+--------------+-----------------------+
| NELSON | BECCA | 1017 | 1012 | 4 |
| GIANA | TAMMY | 1007 | 1007 | 4 |
| MORALES | BONITA | 1001 | 1003 | 3 |
| MORALES | BONITA | 1001 | 1018 | 2 |
| LUCAS | JAKE | 1010 | 1001 | 2 |
| GIRARD | CINDY | 1005 | 1009 | 2 |
| LEE | JASMINE | 1014 | 1013 | 1 |
| MONTIASA | GREG | 1018 | 1005 | 1 |
| MONTIASA | GREG | 1018 | 1019 | 1 |
| PIERSON | THOMAS | 1004 | 1008 | 1 |
| JONES | KENNETH | 1008 | 1020 | 1 |
| MCGOVERN | REESE | 1011 | 1002 | 1 |
| LUCAS | JAKE | 1010 | 1011 | 1 |
| FALAH | KENNETH | 1020 | 1015 | 1 |
| SMITH | JENNIFER | 1019 | 1010 | 1 |
| GIRARD | CINDY | 1005 | 1000 | 1 |
| SMITH | LEILA | 1003 | 1006 | 1 |
| GIANA | TAMMY | 1007 | 1014 | 1 |
| FALAH | KENNETH | 1020 | 1004 | 1 |
| SMITH | LEILA | 1003 | 1016 | 1 |
| SCHELL | STEVE | 1015 | 1017 | 1 |
+------------+------------+------------+--------------+-----------------------+
按照這個截圖...
要提高你的問題的質量,請重現問題的範圍,而不是作爲鏈接的圖像輸出作爲格式化文本。這是因爲不得不導航到鏈接的圖像有點不方便,因爲鏈接可能隨時間而中斷。如果你必須在你的問題中包含圖像,使用'[] [1]'將使你能夠在你的問題中顯示它,而不是作爲鏈接。我在這裏使用了'[]',因爲你不需要輸入圖像描述,但仍然需要括號。如果您使用了多個圖像,請記住將'1'更改爲圖像編號。 – toonice
提供一些示例數據和預期輸出。 – Utsav
理想情況下,它應該返回具有每個訂單號最大數量的商品的行。如果兩個訂單號具有相同數量的項目,則應返回兩行。 –