Sql 练习24-找到价格最高的产品(PC、笔记本电脑或打印机)的型号。结果集:模型

Sql 练习24-找到价格最高的产品(PC、笔记本电脑或打印机)的型号。结果集:模型,sql,alias,Sql,Alias,我有一条SQL语句,它返回了正确的答案,但它太长了,因为我不知道如何重用别名,所以我再次编写了整个SELECT: select model from ( select model, max(price) as maxt from ( select model,price from pc where price in (select max(price) from pc) union select model,price from laptop where price in (select max

我有一条SQL语句,它返回了正确的答案,但它太长了,因为我不知道如何重用别名,所以我再次编写了整个SELECT:

select model from
(
select model, max(price) as maxt from
(
select model,price from pc where price in (select max(price) from pc)
union
select model,price from laptop where price in (select max(price) from laptop)
union
select model,price from printer where price in (select max(price) from printer)
) as ab
group by model
) as ba
where maxt in 

(
select max(maxt) from (

select model, max(price) as maxt from
(
select model,price from pc where price in (select max(price) from pc)
union
select model,price from laptop where price in (select max(price) from laptop)
union
select model,price from printer where price in (select max(price) from printer)
) as aba
group by model
) as bac )
这项工作是: 查找价格最高的产品PC、笔记本电脑或打印机的型号。 结果集:模型

台式笔记本电脑

code    model   speed   ram hd      price       screen
------------------------------------------------------
1       1298    350     32  4.0     700.0000    11
2       1321    500     64  8.0     970.0000    12
3       1750    750     128 12.0    1200.0000   14
4       1298    600     64  10.0    1050.0000   15
5       1752    750     128 10.0    1150.0000   14
6       1298    450     64  10.0    950.0000    12
PC表:

code     model  speed   ram hd  cd  price
-------------------------------------------------------
1   1232    500 64  5.0 12x 600.0000
10  1260    500 32  10.0    12x 350.0000
11  1233    900 128 40.0    40x 980.0000
12  1233    800 128 20.0    50x 970.0000
2   1121    750 128 14.0    40x 850.0000
3   1233    500 64  5.0 12x 600.0000
4   1121    600 128 14.0    40x 850.0000
5   1121    600 128 8.0 40x 850.0000
6   1233    750 128 20.0    50x 950.0000
7   1232    500 32  10.0    12x 400.0000
8   1232    450 64  8.0 24x 350.0000
9   1232    450 32  10.0    24x 350.0000
打印机表:

code    model   color   type    price
-----------------------------------------
1   1276    n   Laser   400.0000
2   1433    y   Jet 270.0000
3   1434    y   Jet 290.0000
4   1401    n   Matrix  150.0000
5   1408    n   Matrix  270.0000
6   1288    n   Laser   400.0000
表产品:

maker   model   Type
-----------------------
A       1232    PC
A       1233    PC
A       1276    Printer
A       1298    Laptop
A       1401    Printer
A       1408    Printer
A       1752    Laptop
B       1121    PC
B       1750    Laptop
C       1321    Laptop
D       1288    Printer
D       1433    Printer
E       1260    PC
E       1434    Printer
E       2112    PC
E       2113    PC

不要把事情过分复杂化。没有必要重用别名。别名是为表或列指定其他名称的一种方式。当你加入两个相同的或类似的表时,要么写的更少,要么区分的更少。你不必那么做。以下是教人钓鱼的方法:

任务:针对每件商品,找到价格最贵的经销商

这个问题可以通过如下子查询解决:

SELECT article, dealer, price
FROM   shop s1
WHERE  price=(SELECT MAX(s2.price)
              FROM shop s2
              WHERE s1.article = s2.article);

+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|    0001 | B      |  3.99 |
|    0002 | A      | 10.99 |
|    0003 | C      |  1.69 |
|    0004 | D      | 19.95 |
+---------+--------+-------+
前面的示例使用了相关子查询,这可能是低效的,请参见第13.2.10.7节“相关子查询”。解决此问题的其他可能性是在FROM子句中使用不相关的子查询或左连接

不相关子查询:

左连接:


左连接的工作原理是,当s1.price处于其最大值时,没有s2.price具有较大的值,并且s2行的值将为空。

我认为此查询与您的查询相同:

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 ab
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 abc) ;

这是一位朋友的回答:

select distinct G1.model from

(select G.model,G.price,rank() over (order by G.price desc) as r from 

(
select a.model,a.price from

(Select model, price,rank() over (order by price desc) as r from pc) a

where a.r = 1

union
select a.model,a.price from 

(Select model, price,rank() over (order by price desc) as r from laptop) a where a.r = 1
union
select a.model,a.price from

(Select model, price ,rank() over (order by price desc) as r from printer) a where a.r = 1
) G

) G1 where G1.r = 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 ab
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 abc) ;
模型 1750年

使用适用于MS SQL server的选项,我认为即使在oracle中,我的anwser也是

with model_price_out as(
select model, price from PC group by model,price having price= max(price)
union
select model, price from Laptop group by model,price having price = max(price)
union 
select model, price from printer group by model,price having price =max(price)
)
select model
from model_price_out
where price >= all( select price from model_price_out)
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 l1
WHERE price = (SELECT TOP(1) 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 l2)
我的回答

Select distinct model from(
select model, price from pc 
union all
select model, price from printer
union all
select model, price from laptop
) x
where price >= all(
select max(price) from pc 
union all
select max(price) from printer
union all
select max(price) from laptop
)

我们可以通过使用秩函数来实现这一点。Bbelow是查询:

select model from
(
select model,price,rank() over (order by price desc) as rnk_1 from
(
select model,price from
(
select model,price,rank() over(order by price desc) as Rnk from laptop 
)a where a.rnk=1
union
select model,price from
(
select model,price,rank() over(order by price desc) as Rnk from pc
)a where a.rnk=1
union
select model,price from
(
select model,price,rank() over(order by price desc) as Rnk from printer 
)a where a.rnk=1
)b 
)c where c.rnk_1=1

解决这项任务的方法有很多,对我来说,这里是最省钱的方法:

select model from
(
select model,price from pc
union all
select model,price from laptop
union all
select model,price from printer
) as A
where price = (select max(price) from
(
select model,price from pc
union all
select model,price from laptop
union all
select model,price from printer
) as B)
还有一个使用:

with max
as 
(select model, price from pc
union 
select model, price from laptop
union 
select model, price from printer)

select model from max
where price = (select max(price) from max)
简短的版本如何?

对于同一个问题: 查找价格最高的打印机型号。结果集:型号、价格

下面的代码似乎也能工作

选择型号、价格 从…起 打印机 哪里 价格=从打印机中选择maxprice 按价格说明订购
您可能希望重新编辑您的问题,并在需要固定布局和语法高亮显示的代码块和其他文本块上使用{}按钮,而不是添加所有这些。预期的结果是什么?一个最昂贵的项目,无论是笔记本电脑、PC或打印机,还是每种产品类型中三行中最昂贵的物品?你在使用哪种DBMS?我的朋友?我正在使用下面网站sql-ex中的练习。rucan无法从你的示例中找出答案。我有三个表,我必须从中提取最高价格的模型。事实上,我有4个表。我需要那个型号的制造商从那些表中提供最高价格。奇怪的是,我有完全相同的查询,而且它无法运行,尽管我发现了我一直在使用UNION的问题
select model from
(
select model,price,rank() over (order by price desc) as rnk_1 from
(
select model,price from
(
select model,price,rank() over(order by price desc) as Rnk from laptop 
)a where a.rnk=1
union
select model,price from
(
select model,price,rank() over(order by price desc) as Rnk from pc
)a where a.rnk=1
union
select model,price from
(
select model,price,rank() over(order by price desc) as Rnk from printer 
)a where a.rnk=1
)b 
)c where c.rnk_1=1
select model from
(
select model,price from pc
union all
select model,price from laptop
union all
select model,price from printer
) as A
where price = (select max(price) from
(
select model,price from pc
union all
select model,price from laptop
union all
select model,price from printer
) as B)
with max
as 
(select model, price from pc
union 
select model, price from laptop
union 
select model, price from printer)

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