2011-07-19 51 views
3

我有一個四表:產品,電腦,筆記本電腦和打印機。使用MAX與多表

Products(maker, model, type) 
    PC(code, model, speed, hd, cd, price) 
    Laptop(code, model, speed, ram, hd, screen, price) 
    Printer(code, model, color, type price) 

我需要的是找到具有最高價格的產品(PC,筆記本電腦或打印機)的型號。這不適用於case語句,因爲如果兩個型號的價格最高,都需要顯示,並且使用一個case只會選擇一個,然後退出case語句。我想用UNION操作符來做到這一點,但我不知道該怎麼做。這是我到目前爲止:

SELECT model FROM 
(SELECT model, MAX(price) FROM 
(SELECT model, price FROM Pc UNION ALL SELECT model, price FROM Laptop UNION ALL 
SELECT model, price FROM Printer) 
GROUP BY model) 

但這是不正確的語法,我不知道爲什麼。有任何想法嗎?

回答

1

你需要你的別名派生表:see this post


編輯:這應該與最高價格來獲取模型。 (我不知道這是否是SQL Server的正確的語法。)

with max_price(price) as (
    SELECT MAX(price) 
     FROM (
      SELECT price 
      FROM Pc 
      UNION ALL 
      SELECT price 
      FROM Laptop 
      UNION ALL 
      SELECT price 
      FROM Printer 
     ) as sub1 
) 

SELECT model, price 
FROM (
    SELECT model, price 
    FROM Pc 
    UNION ALL 
    SELECT model, price 
    FROM Laptop 
    UNION ALL 
    SELECT model, price 
    FROM Printer 
) as sub2 
JOIN max_price m ON m.price = sub2.price 
+0

這解決了語法的問題,但現在我的查詢返回的每一個型號,僅僅是一個不與最高價格.... – nathpilland

+0

差不多,但它說的價格是無效的列名。我需要一張桌子嗎?運營商爲使價格毫不含糊? – nathpilland

+0

我並不完全確定...是什麼,因爲你可以看到我是初學者:/我認爲我理解它,但SQL服務器仍然不喜歡它。它表示沒有爲'max_price'的第1列指定列名稱 – nathpilland

3
Select datatable.model as price from (
    Select P.model,P.price from PC P where P.price=(select Max(Q.price) from PC Q) 
    Union 
    Select P.model,P.price from Laptop P where P.price=(select Max(Q.price) from Laptop Q) 
    Union 
    Select P.model,P.price from Printer P where P.price=(select Max(Q.price) from Printer Q) 
) as datatable where datatable.price=(
    Select Max(newtable.price) as price from (
     Select P.model,P.price from PC P where P.price=(select Max(Q.price) from PC Q) 
     Union 
     Select P.model,P.price from Laptop P where P.price=(select Max(Q.price) from Laptop Q) 
     Union 
     Select P.model,P.price from Printer P where P.price=(select Max(Q.price) from Printer Q)) as newtable) 
-1
select model from (
Select model, price from pc 
where price = (select max(price) from pc) 
union 
Select model, price from laptop 
where price = (select max(price) from laptop) 

union 
Select model, price from printer 
where price = (select max(price) from printer) 
) as G 
where price = (
select max(price) from (
Select model, price from pc 
where price = (select max(price) from pc) 
union 
Select model, price from laptop 
where price = (select max(price) from laptop) 

union 
Select model, price from printer 
where price = (select max(price) from printer) 
) as T 
) 
0
Select b.model from 
(Select a.model, Max(a.price) as price from 
(Select model, MAX(price) as price from PC group by model 
union 
Select model, MAX(price) as price from Laptop group by model 
union 
Select model, MAX(price) as price from Printer group by model)a 
Group by a.model)b 
where b.price=(Select Max(c.price) from(Select model, MAX(price) as price from PC group by model 
union 
Select model, MAX(price) as price from Laptop group by model 
union 
Select model, MAX(price) as price from Printer group by model)c) 
1

這是我的解決辦法,這是工作。我已經試過了。

WITH PRICE_MAX AS (select model, price 
from pc 
where price = (select max(price) 
       from pc) 
UNION 
select model, price 
from laptop 
where price = (select max(price) 
       from laptop) 
UNION 
select model, price 
from printer 
where price = (select max(price) 
       from printer)) 

select model from PRICE_MAX 
where price = (select Max(price) 
       from PRICE_MAX)