的最大值我有這已經工作的SQL語句,但我認爲必須有比我更好的解決方案。SQL查詢:選擇再選擇
我試圖得到的文章與已從未出售的最高價。
有了這個選擇我收到尚未售出的所有文章(數+價格):
select anr, price
from article a
where not exists(
select 1 from OrderItems o
where o.artnr = a.anr
)
的文章編號+價格結果如下
| Anr | Price |
| 1 | 300.0 |
| 4 | 340.0 |
| 5 | 340.0 |
| 3 | 200.0 |
我臨時解決方案獲得價格最高的商品是:
select anr, price
from article
where anr in(
select anr
from article a
where not exists(
select 1 from OrderItems o
where o.artnr = a.anr
)
)
and price = (
select max(price)
from article a
where not exists(
select 1 from OrderItems o
where o.artnr = a.anr
)
)
正確的解決方案是:
| Anr | Price |
| 4 | 340.0 |
| 5 | 340.0 |
有沒有辦法避免兩次使用相同的子查詢?
爲了測試這裏是縮短了與我的插入值創建表的腳本:
CREATE TABLE Article
(
Anr Int Primary Key,
Price Numeric(9,2) Not Null
);
CREATE TABLE Orders
(
OrderNr Int Primary Key
)
CREATE TABLE OrderItems
(
OrderNr Int References Orders On Delete Cascade,
ItemNr Int,
Artnr Int References Article Not Null,
Amount Int Not Null Check(Amount >= 0),
Primary Key(OrderNr, ItemNr)
)
-- articles without an order
Insert into Article (Anr, Price) values(1,300.0);
Insert into Article (Anr, Price) values(4,340.0);
Insert into Article (Anr, Price) values(5,340.0);
Insert into Article (Anr, Price) values(3,200.0);
-- articles for order with orderNr '1'
Insert into Article (Anr, Price) values(2,340.0);
Insert into Article (Anr, Price) values(6,620.0);
-- insert test order that contains the two articles
Insert into Orders (OrderNr) values (1);
Insert into OrderItems(OrderNr, ItemNr, Artnr, Amount) values(1,1,2,4);
Insert into OrderItems(OrderNr, ItemNr, Artnr, Amount) values(1,2,6,2);
我也閱讀主題Select max value in subquery SQL 但我覺得在我的情況下,必須有做選擇較短的方式。
的SQL Server/MySQL的/甲骨文/ PostgreSQL的/火鳥/ SQLite的? – lad2025
它應該適用於每個數據庫,這就是爲什麼我不需要特定的數據庫。我想通過使用標準的SQL來解決這個問題。 但我正在測試Oracle 12c :) – Johnny90
如果您添加腳本以創建表和記錄..我可以幫你 – rdn87