# 查询找到 2 件最便宜的商品，其总价值为 3000

[英]Query to find 2 cheapest items sum value of which gives 3000

I have a table Products:我有一张桌子产品：

``````ProductName          Price
straight jeans       1500
slim jeans           2500
Denim jacket         3000
Denim shorts         800
Skinny jeans         1700
loose Jeans          2100
mom Jeans            2800
wide jeans           1850
distressed jeans     1100
bootcut jeans        1350
``````

For purchased two different things with a total value of 3000 or more, they give a third as a gift.如果购买了两件总价值超过 3000 的不同物品，他们会赠送第三件作为礼物。 I need a SQL query to spend minimum on two things, and take third as the most expensive.我需要一个 SQL 查询，以在两件事上花费最少，并将第三作为最昂贵的。

The only thing I've come up with is to go through all possible combinations and find the cheapest combination that over 3000.我唯一想到的就是通过所有可能的组合到 go 并找到超过 3000 的最便宜的组合。

``````WITH Products_sum AS (
SELECT p1.ProductName AS ProductName1, p2.ProductName AS ProductName2, p1.Price + p2.Price AS TotalPrice
FROM products p1
JOIN products p2
ON p1.ProductName < p2.ProductName
)
SELECT top 1 ProductName1, ProductName2, TotalPrice
FROM Products_sum
WHERE TotalPrice >= 3000
order by TotalPrice asc
``````

``````bootcut jeans        1350
Skinny jeans         1700
Denim jacket         3000
``````

But don't know how to do exactly like that.但是不知道该怎么做。

I continued from where you left it and compose the expected result.我从您离开的地方继续并编写了预期的结果。

Converted as cte the top pair of products and the most expensive.转换为 cte 的顶级产品和最昂贵的产品。

Combine the final resultset with union statement.将最终结果集与 union 语句结合起来。

``````WITH Products_sum AS (
SELECT p1.ProductName AS ProductName1
, p2.ProductName AS ProductName2
, p1.Price + p2.Price AS TotalPrice
, p1.Price Price1
, p2.Price Price2
FROM products p1
JOIN products p2
ON p1.ProductName < p2.ProductName
),
topProducts as (SELECT top 1 ProductName1, ProductName2, Price1, Price2
FROM Products_sum
WHERE TotalPrice >= 3000
order by TotalPrice asc),
moreExpensive as (
select top 1 ProductName, Price
from products
order by price desc
)
select productName, Price from(
select 1 as pos, ProductName1 ProductName, Price1 Price from topProducts
union
select 2, ProductName2 , Price2 from topProducts
union
select 3, ProductName, Price
from moreExpensive )q
order by pos
``````
``````SELECT p1.ProductName, p1.Price,
p2.ProductName, p2.Price,
p3.ProductName, p3.Price
FROM Products p1
JOIN Products p2
on p1.ProductName < p2.ProductName
JOIN Products p3
ON p2.ProductName < p3.ProductName AND
p1.Price + p2.Price + p3.Price > 3000
LEFT JOIN Products nonexistent
ON NOT (nonexistent.ProductName IN (p1.ProductName, p2.ProductName, p3.ProductName)) AND
p1.Price + p2.Price + nonexistent.Price > 3000 AND
nonexistent.Price < p3.Price
WHERE nonexistent.ProductName IS NULL
``````

We我们

• select every ( `p1` , `p2` , `p3` ) tuples select 每个 ( `p1` , `p2` , `p3` ) 元组
• where their name differs他们的名字不同的地方
• their price is greater than 3000他们的价格大于3000
• and there doesn't exist any `nonexistent` record that would differ from these three, would yield a smaller price with `p1` and `p2` than `p3` but still above 3000并且不存在任何与这三个不同的`nonexistent`的记录， `p1``p2`会产生比`p3`更小的价格，但仍高于 3000