选择最小。在JOIN的另一个表中记录的三个值 [英] Select min. three values of a record in another table with a JOIN

查看:101
本文介绍了选择最小。在JOIN的另一个表中记录的三个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表:

表1:Article

  Article_ID价格有效价格ID 
1 2014年8月12日1
1 2014年12月30日2
1 2014年10月10日3
1 2014年10月15日5
2 2014年8月13日1
---(第2条记录的编号)
3 01-July-2014 4
3 02-July-2014 5
---(n条记录第3条)

关键是Article_Id和有效日期。



含义每篇文章有不同的价格,从某些日期有效。一篇文章的日期不能有相同的有效价格。



表2:价格

 价格ID价格
1 10
2 200
3 30
4 400
5 50

要求的结果:

我想为每篇文章最少三个价格。为此,我需要一个连接,但我无法弄清楚如何限制no。每行文章价格为3,以及如何实现最小值。标准。

最终结果应该是:

  Article_ID最低价格1最低价格2 Min Price3 
1 10 30 50
2 10
3 50 400

一篇文章将有最大值。 (最终结果),minimun可能为0.而三种价格是表2中文章的最小价格。



我得到认为它必须用最少的时间做些事情。 (分组)和行号。 (加入表2),但任何人都可以帮助我实现它?



问候
SJ

ROW_NUMBER()将能够给这些数字1,2,3,从那里是一个简单的使用 case表达式与一个组by

3个最低价格:

 选择
a.article_id
,MAX(CASE
当a.price_seq = 1 THEN p.price END)AS price_1
,MAX(CASE
当a.price_seq = 2 THEN p.price END时) AS price_2
,MAX(CASE
当a.price_seq = 3 THEN p.price END时)AS price_3
FROM(
SELECT
article_id
,price_valid_from
,price_id
,ROW_NUMBER()OVER(PARTITION BY article_id
ORDER BY p.price ASC)AS price_seq
FROM article
LEFT OUTER JOIN价格p
ON(a.price_id = p.price_id)
)a
GROUP BY
a.article_id
ORDER BY
a.article_id

for 3最近的价格

  SELECT 
a.article_id
,MAX(CASE
当.price_seq = 1 THEN p.price END)AS price_1
,MAX(CASE
当a.price_seq = 2 THEN p.price END)AS price_2
,MAX(CASE
WHEN a.price_seq = 3 THEN p.price END)AS price_3
FROM(
SELECT
article_id
,price_valid_from
,price_id
,ROW_NUMBER() OVER(PARTITION BY article_id
ORDER BY price_valid_from DESC)AS price_seq
从文章
)a
LEFT OUTER JOIN价格p
ON(a.price_id = p.price_id)
GROUP BY
(a.article_id)
ORDER BY
a.article_id

Demo SQLfiddle


I have two tables:

Table 1: Article

Article_ID         Price Valid from         Price ID        
1                  12-Aug-2014              1
1                  30-Dec-2014              2
1                  10-Oct-2014              3
1                  15-Oct-2014              5
2                  13-Aug-2014              1
---(n no. of records for article 2)
3                  01-July-2014             4   
3                  02-July-2014             5   
---(n no. of records for article 3)   

Here the unique key is Article_Id and Valid from date.

Meaning each article have different prices which are valid from certain dates. An article can not have different prices on the same valid from date.

Table 2: Price

Price ID            Price
1                   10
2                   200
3                   30
4                   400
5                   50

Required result:

I want for each article Minimum three Prices. For this I need a join, but I am not able to figure out how to restrict the no. of rows for each article Price to 3, and how to implement the min. criteria.

Final result should be:

Article_ID                     Min Price1      Min Price2    Min Price3
1                              10              30            50
2                              10    
3                              50              400

An article will have max. of three prices (in the final result), minimun could be 0. And three Prices it has are the smallest prices of the article from the table 2.

I get the idea that it has to do something with min. (Group by) and row no. (with a join on Table 2), but can anyone help me with it's implementation?

Regards SJ

解决方案

If the requirement remains for a pivot of 3 prices in a series, then using ROW_NUMBER() will enable giving these numbers 1, 2, 3 and from there is is a simple use of case expressions with a group by:

for 3 lowest prices:

SELECT
      a.article_id
    , MAX(CASE
            WHEN a.price_seq = 1 THEN p.price END) AS price_1
    , MAX(CASE
            WHEN a.price_seq = 2 THEN p.price END) AS price_2
    , MAX(CASE
            WHEN a.price_seq = 3 THEN p.price END) AS price_3
FROM (
            SELECT
                  article_id
                , price_valid_from
                , price_id
                , ROW_NUMBER() OVER (PARTITION BY article_id
                                     ORDER BY p.price ASC) AS price_seq
            FROM article
                  LEFT OUTER JOIN price p
                              ON (a.price_id = p.price_id)
      ) a
GROUP BY
      a.article_id
ORDER BY
      a.article_id

for 3 most recent prices

SELECT
      a.article_id
    , MAX(CASE
            WHEN a.price_seq = 1 THEN p.price END) AS price_1
    , MAX(CASE
            WHEN a.price_seq = 2 THEN p.price END) AS price_2
    , MAX(CASE
            WHEN a.price_seq = 3 THEN p.price END) AS price_3
FROM (
            SELECT
                  article_id
                , price_valid_from
                , price_id
                , ROW_NUMBER() OVER (PARTITION BY article_id
                                     ORDER BY price_valid_from DESC) AS price_seq
            FROM article
      ) a
      LEFT OUTER JOIN price p
                  ON (a.price_id = p.price_id)
GROUP BY
      (a.article_id)
ORDER BY
      a.article_id

Demo SQLfiddle

这篇关于选择最小。在JOIN的另一个表中记录的三个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆