SQL Server 查询2个最便宜的项目,其总值为3000

li9yvcax  于 2023-02-03  发布在  其他
关注(0)|答案(3)|浏览(125)

我有一个表产品:

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

对于购买两个不同的东西,总价值3000或以上,他们给予第三个作为礼物。我需要一个SQL查询花费最少的两件事,并采取第三个作为最昂贵的。
我唯一想到的是通过所有可能的组合,找到最便宜的组合,超过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

但不知道该怎么做。

jslywgbw

jslywgbw1#

我从你离开的地方继续,并组成预期的结果。
转换为cte的顶级对产品和最昂贵的。
将最终结果集与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
huwehgph

huwehgph2#

这将给你独特的组合通过一个交叉连接和一个小逻辑开关。

    • 示例**
;with cte as (
Select Distinct
       Prod1  = ProductName
      ,Price1 = Price
      ,Prod2  = ''
      ,Price2  =0
 From YourTable A
 Where PrIce>=3000
Union All
Select Distinct
       Prod1  = case when A.ProductName > B.ProductName then A.ProductName else B.ProductName end
      ,Price1 = case when A.ProductName > B.ProductName then A.Price       else B.Price end
      ,Prod2  = case when A.ProductName > B.ProductName then B.ProductName else A.ProductName end
      ,Price2 = case when A.ProductName > B.ProductName then B.Price       else A.Price end
 From YourTable A
 Cross Join YourTable B
 Where A.Price+B.Price>=3000
   and A.ProductName<>B.ProductName
)
Select *
      ,TotalPrice = Price1+Price2
 From  cte
 Order By Price1+Price2
    • 结果**

zujrkrfu

zujrkrfu3#

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

我们

  • 选择每个(p1p2p3)元组
  • 他们的名字不同
  • 他们的价格高于3000
  • 并且不存在与这三个记录不同的任何nonexistent记录,则p1p2将产生比p3更小的价格,但是仍然高于3000

相关问题