我有这个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
答案 0 :(得分:2)
我认为此查询与您的查询相同:
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) ;
答案 1 :(得分:2)
解决任务有很多方法,这对我来说是最省钱的:
implementation (project(path: ":coreLib")) {
exclude group: "org.apache.xmlbeans"
}
以及使用WITH的另一种方法:
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)
答案 2 :(得分:1)
<强> (Source) The Rows Holding the Group-wise Maximum of a Certain Column: 强>
任务:对于每篇文章,找到价格最贵的经销商或经销商。
这个问题可以通过像这样的子查询来解决:
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子句或LEFT JOIN中使用不相关的子查询。
不相关的子查询:
SELECT s1.article, dealer, s1.price
FROM shop s1
JOIN (
SELECT article, MAX(price) AS price
FROM shop
GROUP BY article) AS s2
ON s1.article = s2.article AND s1.price = s2.price;
LEFT JOIN:
SELECT s1.article, s1.dealer, s1.price
FROM shop s1
LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price < s2.price
WHERE s2.article IS NULL;
LEFT JOIN的工作原理是,当s1.price处于最大值时,没有s2.price具有更大的值,s2行值将为NULL。
答案 3 :(得分:1)
WITH ALL_PRODUCTS
AS
(SELECT MODEL,PRICE FROM PC
UNION
SELECT MODEL,PRICE FROM LAPTOP
UNION
SELECT MODEL,PRICE FROM PRINTER)
SELECT
MODEL FROM ALL_PRODUCTS
WHERE PRICE = (SELECT MAX(PRICE) FROM ALL_PRODUCTS)
右。
您的查询结果:
MODEL 1750
使用选项适用于MS SQL服务器,我认为即使在oracle
答案 4 :(得分:1)
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)
简短版本如何?
答案 5 :(得分:0)
这个答案是由一位朋友给出的:
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
答案 6 :(得分:0)
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)
答案 7 :(得分:0)
with pro as
(select model,price from PC where price=(select MAX(price) from PC)
UNION ALL
select model,price from Laptop where price=(select MAX(price) from Laptop)
UNION ALL
select model,price from Printer where price=(select MAX(price) from Printer) )
select model from pro where price>= (select MAX(price) from pro) group by model
答案 8 :(得分:0)
我的回答是
public class NewTransactionEventHandler<T> implements TransactionEventHandler<T>
{
@Override
public T beforeCommit(TransactionData transactionData)
答案 9 :(得分:0)
我的回答
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
)
答案 10 :(得分:0)
我们可以通过使用Rank函数来实现.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
答案 11 :(得分:0)
对于同一问题: 查找价格最高的打印机型号。结果集:型号,价格。
以下代码似乎也可以正常工作
select model, price
from
printer
where
price = (select max(price) from printer)
order by price desc