2017-10-21 103 views
0

選擇列SQL頂行我有2個表無法從嵌套查詢

產品

PID NAME    ORIGINAL_PRICE DISCNT_RATE 
---- -----------   -------------- ----------- 
p001 stapler      9.99   .1 
p002 TV       249   .15 
p003 camera      148   .2 

購買

PUR# EID PID CID   QTY PTIME  TOTAL_PRICE 
------ --- ---- ---- ---------- --------- ----------- 
100001 e01 p002 c001   1 12-AUG-17  211.65 
100002 e01 p003 c001   1 20-SEP-17  118.4 
100003 e02 p004 c002   5 08-OCT-17  4.95 
100005 e04 p007 c004   1 15-OCT-17  119.2 
100006 e03 p008 c001   1 12-OCT-17  349.3 

找到已被出售的每個產品的PID和名稱就總量而言最多。顯示相應的總量(標題:「已售出的總量」)。使用列格式將標題更改爲「已售出總量」。

溶膠:

我做了什麼至今

SELECT Max("Total") AS "HS", pid 
from (Select pid, sum(qty) as "Total" 
from purchases group by pid); 

我無法處理預期如果任何幫助,可以提供的信息..

PS:它不是作業

+0

你能分享你正在使用生成數據的SQL? –

回答

0

您可以使用Sum和group by來獲得總計值和TOP 1的訂購結果以獲取最大值

在SQL Server

select TOP 1 a.PID, sum(b.QTY) as "total quantity sold" 
    FROM Products AS a 
    INNER JOIN Purchases AS b on a.PID = b.PID 
    GROUP BY a.pID 
    ORDER BY sum(b.QTY) DESC 

使用從子選擇最大爲你做了..可以產生錯誤的結果,因爲在大多數的分貝,這是不允許的或(不通過獲得一排只有使用組)可以顯示一個PID的結果集的子查詢的隨便獲得..

,如果你不想使用TOP(限行),你應該使用過濾器機智的最大值合適,其中子查詢和獲取正確的情侶PID,MAX tital

0

對於銷售量最大的產品的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 | 
+0

你的問題現在解決了嗎?你仍然有關於這個答案的問題嗎?接受並回答 「[**點擊蜱**](https://ibb.co/ikqyO6)」 的詳細看[求助/接收(https://stackoverflow.com/help/someone-answers) –