1

继我今天早些时候的帖子之后,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 提出了以下完美的解决方案。但是我现在意识到我还需要返回定价描述列的值(fullDay、perWeek、morning 等)。我仍然只需要为每个boat_prices 行返回最低价格,但我还需要知道该行的pricingDescription 值。我尝试使用下面的示例解决它,但我很难过。您可以提供的任何帮助将不胜感激。所以理想情况下,我会在语句的嵌套部分有以下解决方案:

select pricingref, min(price) as min_price, pricingDescription 

***UPDATE - seasonDescription 列意味着,在某些情况下,会有多行价格相同(参见上例中的第 16、17 和 18 行)。价格可能会在 seasonDescriptions 之间重复,但不会在相同的pricingRef 的pricingDescription 之间重复。

谢谢,

杰森

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

4 回答 4

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;
于 2013-07-04T12:46:54.363 回答
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;
于 2013-07-04T12:30:36.223 回答
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

您可以将要为 min(price) 分组的所有字段按部分放在分区中。

于 2013-07-04T12:45:52.990 回答
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
于 2013-07-04T12:41:44.457 回答