从每个类别

时间:2017-04-17 19:21:16

标签: sql sql-server sql-server-2014

我有3张桌子:

  • 产品:id,名称
  • ProductPrices:id,ProductId,PriceListId,Value,AddDateTime
  • PriceLists:id,Title

我可以使用ProductId和PriceListId

为ProductPrices中的每个产品添加多个值

现在我想从ProductPrices中选择产品ID,但不是所有产品ID,从当前产品的每个价目表中选择前2个值。

我该怎么做?

我写了这段代码:但它返回所有价格

Select PR.PriceListId, PL.Title, PR.id,  PR.ProductId, PR.Value,  PR.AddDateTime
From Products FL left outer join
     ProductPrices PR
     on PR.ProductId = FL.id left outer join
     PriceLists PL
     on PR.PriceListId = PL.id  
group by PR.id , PR.ProductId, PR.Price, PR.AddDateTime, PL.Title , PR.PriceListId 
order by Pr.PriceListId 

编辑:

    Products:
    id   Name 
    1    Product1 
    2    Product2 

    ProductPrices: 
    id    ProductId    PriceListId    Value    AddDateTime
    1     1            1              XXX      Today
    2     1            1              XXX      YesterDay
    3     1            1              XXX      Older than yesterday
    4     1            2              XXX      Today
    5     1            3              XXX      Today
    6     1            3              XXX      Today
    7     1            3              XXX      YesterDay
    9     2            1              XXX      YesterDay
    11    2            2              XXX      Today
    12    2            3              XXX      Today
    13    2            3              XXX      Today
    14    2            3              XXX      YesterDay

    PriceLists:
    id     Title 
    1      X 
    2      Y
    3      Z

Result from ProductPrices By id should be:

id    ProductId    PriceListId    Value    AddDateTime
1     1            1              XXX      Today
2     1            1              XXX      YesterDay
4     1            2              XXX      Today
5     1            3              XXX      Today
6     1            3              XXX      Today
9     2            1              XXX      YesterDay
11    2            2              XXX      Today
12    2            3              XXX      Today
13    2            3              XXX      Today

2 个答案:

答案 0 :(得分:0)

这应该有效,假设您希望每件商品的价格最高。如果您想要最低,请在row_number函数中更改为“按值排序asc”。

Select PR.PriceListId, PL.Title, PR.id, PR.ProductId, PR.Value, PR.AddDateTime
From Products FL
left outer join (select * from (
                 select *, row_number() over (order by AddDateTime desc) as row_num 
                 from ProductPrices) 
                 where row_num <= 2) PR 
on PR.ProductId = FL.id
left outer join PriceLists PL on PR.PriceListId = PL.id
group by PR.id, PR.ProductId, PR.Value, PR.AddDateTime, PL.Title, PR.PriceListId 
order by Pr.PriceListId 

答案 1 :(得分:0)

使用outer apply

很有趣
Select PR.PriceListId, PL.Title, PR.id,  PR.ProductId, PR.Value, PR.AddDateTime
From Products FL outer apply
     (select top 2 PR.*
      from ProductPrices PR
      where PR.ProductId = FL.id
      order by PR.value desc  -- presumably you want the "top 2" by value
     ) PL left join
     PriceLists PL
     on PR.PriceListId = PL.id
order by Pr.PriceListId ;

我猜不再需要聚合。