SQL查询 - 加入两个表,只返回最低价格续

时间:2013-07-04 12:22:36

标签: sql sql-server

继我今天早些时候的帖子之后,a_horse_with_no_name非常迅速地回答了这个问题。 (非常感谢你)我现在意识到我需要从boat_prices表中返回一个额外列的值。作为回顾:

我有两个MSSQL表如下:

boat_data

boat_prices

船数据包含有关船只的大量数据。每艘船都有一个独特的ID(pricingRef)船价为每艘船提供各种租赁价格,每艘船可以有任意数量的行。每行还有一个pricingRef值。 boat_prices表中的示例数据可能是:

ID > pricingRef > pricingDescription > seasonDescription > price

1 > ASD1 > fullDay > lowSeason > 1500
2 > ASD1 > fullDay > midSeason > 1800
3 > ASD1 > fullDay > highSeason > 2000
4 > ASD1 > perWeek > lowSeason > 9000
5 > ASD1 > perWeek > midSeason > 10000
6 > ASD1 > perWeek > highSeason > 11000
7 > ASD1 > morning > lowSeason > 800
8 > ASD1 > morning > midSeason > 1000
9 > ASD1 > morning > highSeason > 1100
10 > ASD2 > fullDay > lowSeason > 2000
11 > ASD2 > fullDay > midSeason > 2200
12 > ASD2 > fullDay > highSeason > 2400
13 > ASD2 > perWeek > lowSeason > 12000
14 > ASD2 > perWeek > midSeason > 14000
15 > ASD2 > perWeek > highSeason > 16000
16 > ASD2 > morning > lowSeason > 1000
17 > ASD2 > morning > midSeason > 1000
18 > ASD2 > morning > highSeason > 1000

等。等

我需要做的是从boat_data表中取回所有行并将其加入到boat_prices表中,只返回每艘船的最低价格。 a_horse_with_no_name提出了以下解决方案,它完美无缺。但是我现在意识到我还需要返回pricingDescription列的值(fullDay,perWeek,morning等)。我仍然只需要为每个boat_prices行返回最低价格,但我还需要知道该行的pricingDescription值。我试图用下面的例子解决它,但我很难过。我们将非常感谢您提供的任何帮助。理想情况下,我在语句的嵌套部分有以下解决方案:

select pricingref, min(price) as min_price, pricingDescription 

***更新 - seasonDescription列表示在某些情况下,会有多行具有相同的价格(请参阅上例中的第16,17和18行)。价格可能会在seasonDescriptions中重复出现,但在相同的pricingRef。

之间从不会出现在priceDescription上

谢谢,

杰森

select boat_data.*,
   t.min_price
from boat_data
join (
 select pricingref, min(price) as min_price
 from boat_prices
 group by pricingref
) t on t.pricingref = boat_data.pricingref;

4 个答案:

答案 0 :(得分:3)

select boat_data.*,
       t.price, 
       t.pricingDescription
from boat_data
join (
  select pricingref, 
         price, 
         pricingDescription, 
         row_number() over (partition by pricingref order by price) as rn
  from boat_prices
) t 
  on t.pricingref = boat_data.pricingref 
 and t.rn = 1;

答案 1 :(得分:0)

你试过这个吗?

select boat_data.*,
   t.min_price,t.pricingDescription 
from boat_data
join (
 select pricingref, min(price) as min_price,pricingDescription 
 from boat_prices
 group by pricingref,pricingDescription 
) t on t.pricingref = boat_data.pricingref;

答案 2 :(得分:0)

试试这个:

select 
    boat_data.*,
    b.pricingDescription,
    t.min_price
from boat_data
inner join (
    select pricingref, min(price) as min_price
    from boat_prices
    group by pricingref
) t on t.pricingref = boat_data.pricingref
inner join boat_prices b on (b.price = t.min_price and b.pricingRef = t.pricingRef)

编辑:我根据评论中的信息添加了另一种解决方案。此版本应返回pricingRef和seasonDescription的每个组合的最低价格(如果在几个季节中出现相同价格,则为多行):

select 
    boat_data.*,
    t.seasonDescription,
    pricingDescription,
    t.min_price
from boat_data
 join (
    select pricingref, seasonDescription, min(price) as min_price
    from boat_prices
    group by pricingref, seasonDescription
) t on t.pricingref = boat_data.pricingref
 join boat_prices b on b.price = t.min_price and b.pricingRef = t.pricingRef and t.seasonDescription = b.seasonDescription
order by pricingRef

答案 3 :(得分:0)

您可以使用以下内容:

select *
from
(
    select boat_data.boatDataField1,
           boat_data.boatDataField2,
           boat_data.boatDataField3,
           boat_data.boatDataField4,
           boat_prices.price,
           min(boat_prices.price) over (partition by boat_data.pricingref) as min_price,
           boat_prices.pricingDescription
    from boat_data
    join boat_prices on boat_prices.pricingref = boat_data.pricingref
)
where price = min_price

您可以将所有要填写的字段添加到'按分区划分你的分钟(价格)。