我寫下面的查詢是沒有任何錯誤消息但是我看到的問題與輸出返回輸出:錯誤與組由在相關子查詢內的查詢
select productid, productname, categoryid, unitprice
FROM production.products as PP
where unitprice in (select min(unitprice) as minprice
from production.products as PC
group by categoryid)
order by categoryid
go
結果:
24 Product QOGNU 1 4.50
3 Product IMEHJ 2 10.00
19 Product XKXDO 3 9.20
21 Product VJZZH 3 10.00
33 Product ASTMN 4 2.50
52 Product QSRXF 5 7.00
54 Product QAQRL 6 7.45
74 Product BKAZJ 7 10.00
13 Product POXFU 8 6.00
輸出顯示categoryid = 3的多行。當我們按categoryid分組時,不應該顯示每個categoryid只有一行(一分鐘單價)。
我哪裏錯了? 在此先感謝大家的幫助。
您只在子查詢中分組。此外,您的'IN'與每個價格相比不是產品類別的價格。選中Tim回答,看看如何使用'JOIN' –