SQL Developer QUERY用于单分配

时间:2018-09-30 00:12:05

标签: sql oracle-sqldeveloper

我被以下查询困扰。有人可以帮忙吗?

  1. 显示订购最多的客户的全名和电话号码。

  2. 哪个项目最畅销?显示说明和价格。

附有我的ERD。

创建表语句如下:

CREATE TABLE ITEMS (
itemID number(5),
itemDescription varchar2(60),
itemSize varchar2(22),       
itemColour varchar2(10),
itemPrice number (5,2),
itemQuantityAvailable number(3),
CONSTRAINT pk_items PRIMARY KEY (itemID));

CREATE TABLE CUSTOMERS (
custID number(5),
custLName varchar2(10),
custFName varchar2(20),
custAddress varchar2(30),
custTown varchar2(20),
custPostcode number(4),
custPhone number(10),
custEmail varchar2(30),
shopID number(5),
CONSTRAINT pk_customers PRIMARY KEY (custID),
CONSTRAINT fk_customers_shopID FOREIGN KEY (shopID)
REFERENCES SHOPS (shopID));

CREATE TABLE ORDERS (
orderNo number(5),
orderDate Date,
dispatchDate date,
custID number(5),
CONSTRAINT pk_orders PRIMARY KEY (orderNo),
CONSTRAINT fk_orders_custID FOREIGN KEY (custID)
REFERENCES CUSTOMERS (custID));

CREATE TABLE OrderQuantity (
orderNo number(5),
itemID number(5),
orderQuantity number(3),
CONSTRAINT pk_orderQty PRIMARY KEY (itemID, orderNo),
CONSTRAINT fk_orderQty_itemID FOREIGN KEY (itemID)
REFERENCES ITEMS (itemID),
CONSTRAINT fk_orderQty_orderNo FOREIGN KEY (orderNO)
REFERENCES ORDERS (orderNo));

插入语句如下:

INSERT INTO ITEMS VALUES (1,'ADIDAS MEN''S ID STADIUM FULL-ZIP JACKET','S/M/L/XL','Silver',89.99,100);
INSERT INTO ITEMS VALUES (2,'ADIDAS MEN''S ESSENTIALS 3 STRIPES CREW FRENCH TERRY','S/L/XL','Black',71.99,90);
INSERT INTO ITEMS VALUES (3,'ADIDAS MEN''s ESSENTIALS 3-STRIPES FLEECE PANT','M/L/XL/XXL','White',62.99,80);
INSERT INTO ITEMS VALUES (4,'NIKE MEN''S FULL-ZIP SPORTSWEAR HOODIE','M/L/XL','DarkGrey',67.49,70);
INSERT INTO ITEMS VALUES (5,'NIKE MEN''S HBR FLEECE CREW','S/L/XL','DarkGrey',58.49,60);
INSERT INTO ITEMS VALUES (6,'PUMA MEN''S ESSENTIAL SWEAT PANTS','XS/S/M/L/XL','Navy',45.00,50);
INSERT INTO ITEMS VALUES (7,'ADIDAS MEN''S ESSENTIALS CHELSEA SHORTS','M/L/XL','Green',35.99,40);
INSERT INTO ITEMS VALUES (8,'PUMA MEN''S CORE 7IN RUNNING SHORTS','S/L/XL','Black',26.99,20);
INSERT INTO ITEMS VALUES (9,'ASICS GT 2000 6 MEN''S RUNNING SHOES','8/8.5/9/9.5/10','Silver',239.00,90);
INSERT INTO ITEMS VALUES (10,'ASICS GEL-KAYANO 25 2E WIDE MEN''S RUNNING SHOE','9/9.5/10/10.5/11','Black',219.00,60);
INSERT INTO ITEMS VALUES (11,'REEBOK RUNNER 3.0 MEN''S RUNNING SHOES','8/8.5/10/10.5','Orange',209.00,10);
INSERT INTO ITEMS VALUES (12,'REEBOK MEN''S WORKOUT READY 2.0 TEE','XS/S/M','White',199.00,50);
INSERT INTO ITEMS VALUES (13,'UNDER ARMOUR RAPID MEN''S RUNNING SHOES','8.5/10/11','Black',199.00,20);
INSERT INTO ITEMS VALUES (14,'UNDER ARMOUR CHARGED MEN''S RUNNING SHOES','8/8.5/9/9.5/10','Silver',159.00,80);
INSERT INTO ITEMS VALUES (15,'ADIDAS SOLAR DRIVE MEN''S RUNNING SHOE','8/8.5/10/10.5','Black',139.00,30);


INSERT INTO CUSTOMERS VALUES (1,'Widimer','Cindy','121 King St','Box Hill',3195,0470121566,'cindy_widimer@gmail.com',1);
INSERT INTO CUSTOMERS VALUES (2,'Bohrman','Robert','54 Queens St','Murrumbeena',3195,0455131565,'robert_bohram@gmail.com',3);
INSERT INTO CUSTOMERS VALUES (3,'Clarke','Brenda','89 Pecket St','Oakleigh',3195,0424456123,'brenda.clarke@gmail.com',2);
INSERT INTO CUSTOMERS VALUES (4,'Cartier','Bruce','12 Parkore St','Mulgrave',3195,0425654123,'bruce.cartier@gmail.com',3);
INSERT INTO CUSTOMERS VALUES (5,'Heart','Lucy','45 Rose St','Hampton Park',3178,0470456321,'lucy.heart@gmail.com',1);
INSERT INTO CUSTOMERS VALUES (6,'Carter','Joan','123 Davdison St','Hampton Park',3178,0452123789,'carter_joangmail.com',3);
INSERT INTO CUSTOMERS VALUES (7,'John','Ailene','56 Spring St','Yaraman',3178,0475123159,'john.ailene@gmail.com',1);
INSERT INTO CUSTOMERS VALUES (8,'Lewis','Carl','45 Brendon Ct','Noble Park',3178,0454789456,'carl_lewis@gmail.com',3);
INSERT INTO CUSTOMERS VALUES (9,'Holden','Hilary','56 Swanston St','Sunshine',3020,0421456123,'hilar_holdengmail.com',1);
INSERT INTO CUSTOMERS VALUES (10,'Powell','Venus','124 Russel St','Richmond',3112,0431456753,'venus_powellgmail.com',2);
INSERT INTO CUSTOMERS VALUES (11,'Juliet','Susan','87 Fawkner Ct','South Yara',3141,0424687423,'susan_juliet@gmail.com',2);
INSERT INTO CUSTOMERS VALUES (12,'Price','Victor','125 William St','Flagstaff',3003,0465789123,'victor_price@gmail.com',2);


INSERT INTO ORDERS VALUES (1,'01/Jan/18','03/Feb/18',1);
INSERT INTO ORDERS VALUES (2,'20/Jan/18','05/Feb/18',3);
INSERT INTO ORDERS VALUES (3,'05/Feb/18','10/Feb/18',4);
INSERT INTO ORDERS VALUES (4,'18/Feb/18','22/Feb/18',5);
INSERT INTO ORDERS VALUES (5,'01/Mar/18','10/Mar/18',10);
INSERT INTO ORDERS VALUES (6,'12/Mar/18','15/Mar/18',12);
INSERT INTO ORDERS VALUES (7,'07/Apr/18','14/Apr/18',11);
INSERT INTO ORDERS VALUES (8,'25/Apr/18','27/Apr/18',3);
INSERT INTO ORDERS VALUES (9,'06/May/18','10/May/18',2);
INSERT INTO ORDERS VALUES (10,'17/Jun/18','20/Jun/18',5);
INSERT INTO ORDERS VALUES (11,'29/Jun/18','07/Jul/18',4);
INSERT INTO ORDERS VALUES (12,'08/Jul/18','14/Jul/18',11);
INSERT INTO ORDERS VALUES (13,'25/Jul/18','29/Jul/18',12);
INSERT INTO ORDERS VALUES (14,'01/Aug/18','18/Aug/18',5);
INSERT INTO ORDERS VALUES (15,'19/Aug/18','10/Sep/18',1);
INSERT INTO ORDERS VALUES (16,'11/Sep/18','15/Sep/18',4);
INSERT INTO ORDERS VALUES (17,'21/Sep/18','29/Sep/18',5);
INSERT INTO ORDERS VALUES (18,'01/Oct/18',null,10);
INSERT INTO ORDERS VALUES (19,'20/Oct/18',null,3);
INSERT INTO ORDERS VALUES (20,'23/Oct/18',null,1);

INSERT INTO OrderQuantity VALUES (1,1,20);
INSERT INTO OrderQuantity VALUES (2,3,10);
INSERT INTO OrderQuantity VALUES (3,4,20);
INSERT INTO OrderQuantity VALUES (4,5,14);
INSERT INTO OrderQuantity VALUES (5,10,10);
INSERT INTO OrderQuantity VALUES (6,11,15);
INSERT INTO OrderQuantity VALUES (7,12,10);
INSERT INTO OrderQuantity VALUES (8,3,25);
INSERT INTO OrderQuantity VALUES (9,2,50);
INSERT INTO OrderQuantity VALUES (10,5,20);
INSERT INTO OrderQuantity VALUES (11,4,10);
INSERT INTO OrderQuantity VALUES (12,11,20);
INSERT INTO OrderQuantity VALUES (13,12,10);
INSERT INTO OrderQuantity VALUES (14,5,10);
INSERT INTO OrderQuantity VALUES (15,1,10);
INSERT INTO OrderQuantity VALUES (16,4,30);
INSERT INTO OrderQuantity VALUES (17,5,9);
INSERT INTO OrderQuantity VALUES (18,10,30);
INSERT INTO OrderQuantity VALUES (19,3,20);
INSERT INTO OrderQuantity VALUES (20,1,15);

2 个答案:

答案 0 :(得分:0)

检查此sqlfiddle http://sqlfiddle.com/#!5/553399/39/0

订购最多的客户的全名和电话号码:

SELECT ORDERS.custID,CUSTOMERS.custFName,CUSTOMERS.custPhone,COUNT(ORDERS.orderNo ) 
FROM ORDERS  inner join CUSTOMERS
on CUSTOMERS.custID =ORDERS.custID 
group by ORDERS.custID,CUSTOMERS.custFName,CUSTOMERS.custPhone
order by COUNT(ORDERS.orderNo ) desc

畅销书商品(数量)的描述和价格:

SELECT OrderQuantity.itemID ,ITEMS.itemDescription,ITEMS.itemPrice ,sum(distinct OrderQuantity.itemID ) 
FROM OrderQuantity inner join ITEMS 
on OrderQuantity.itemID =ITEMS.itemID 
GROUP BY  OrderQuantity.itemID ,ITEMS.itemDescription,ITEMS.itemPrice
ORDER BY sum(distinct OrderQuantity.itemID) DESC

如果您只需要畅销书,则可以在这些查询中添加rownum 1

select * from (SELECT OrderQuantity.itemID ,ITEMS.itemDescription,ITEMS.itemPrice ,sum(distinct OrderQuantity.itemID ) 
FROM OrderQuantity inner join ITEMS 
on OrderQuantity.itemID =ITEMS.itemID 
GROUP BY  OrderQuantity.itemID ,ITEMS.itemDescription,ITEMS.itemPrice
ORDER BY sum(distinct OrderQuantity.itemID) DESC) q
Where rownum=1

答案 1 :(得分:0)

这是一个简单的示例,说明如何通过进行子查询并仅从其中选择第一行来解决该问题。

SELECT full_name, custPhone 
FROM (SELECT CONCAT(custFName, ' ', custLName) as  full_name, custPhone, COUNT(o.orderNo) num_orders
      FROM ORDERS o
      JOIN Customers c ON c.custId = o.custid
      GROUP BY full_name, custPhone
      ORDER BY num_orders Desc ) AS subq
WHERE ROWNUM = 1

与第二个查询相同

SELECT itemDescription, itemPrice
FROM (SELECT itemDescription, itemPrice, SUM(orderQuantity) sum_quantity
      FROM OrderQuantity o
      JOIN Items i ON i.itemId = o.itemId
      GROUP BY itemDescription, itemPrice
      ORDER BY sum_quantity DESC) AS subq
WHERE ROWNUM = 1