我有四张桌子:产品,个人电脑,笔记本电脑和打印机。
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语句。我想使用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)
但这是不正确的语法,我不知道为什么。有什么想法吗?
答案 0 :(得分: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 :(得分: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
答案 2 :(得分: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)
答案 3 :(得分: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)
答案 4 :(得分:-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
)