2013-05-16 81 views
0

編輯試圖列出具有任何其他項的數量大於同樣的工作子查詢使用條件選擇

Table items 
+--------+----------+ 
| job_id | quantity | 
+--------+----------+ 
| 004 |  150 | 
| 004 |  4 | 
| 004 |  100 | 
| 002 |  50 | 
| 002 |  2 | 
| 002 |  17 | 
| 002 |  17 | 
| 006 |  2 | 
+--------+----------+ 

我知道它應該是一個相當簡單的所有項目,但它讓我卡住。我試圖在查詢中使用ANY,但我不太確定如何使用它。這是我在想什麼:

select job_id, quantity 
from items 
where quantity > 
    (select min(quantity) from items) group by job_id; 

我是那樣的。感謝您的幫助

select job_id, max(quantity) from po_items group by job_id; 

那人做的伎倆

回答

1

試試這個:

select job_id, quantity 
from items 
where quantity > (select MAX(quantity) from items where ...); 
0

您可以使用功能MAX()

您可以使用select MAX(quantity) from items where

select job_id, quantity 
from items 
where quantity > (select MAX(quantity) from items where ...); 
0

PSR在正確的道路上。

對於每個作業的最大值,可以使用聚合MAX()函數,最有效地使用GROUP BY。

嘗試:

select job_id, MAX(quantity) from items GROUP BY job_id

(從手機發送)

+0

感謝它的伎倆 – Tom

0

沒有SUBQUERY。檢查SQLFIDDLE

SELECT 
    job_id, 
    MAX(quantity) 
FROM items 
GROUP BY job_id