3

我有四张桌子:产品、电脑、笔记本电脑和打印机。

    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)

但这是不正确的语法,我不知道为什么。有任何想法吗?

4

5 回答 5

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)
于 2012-05-09T07:42:18.290 回答
1

您需要为派生表起别名:请参阅此帖子


编辑:这应该可以得到最高价格的模型。(我不确定这是否是 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
于 2011-07-19T20:25:32.723 回答
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)
于 2014-06-11T09:06:44.540 回答
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)
于 2014-01-24T17:59:49.403 回答
-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
)
于 2013-06-26T10:08:16.223 回答