新表中每个项目的前3名 - postgres

时间:2013-11-23 19:26:57

标签: sql postgresql

我有下面的表1。对于每种产品,我希望3家顶级超市以下降顺序考虑价格。想要的结果如下表2所示。我尝试了一些SQL查询,但没有一个解决了我的问题。谢谢,

表1

    Product                        |Supermarket                        |Price
-----------------------------------+-----------------------------------+-----------------
    Bean                           |Adonis                             |87
    Coca-Cola                      |Adonis                             |24
    Butter                         |Adonis                             |60
    Bean                           |Emynia                             |43
    Coca-Cola                      |Emynia                             |84
    Butter                         |Emynia                             |50
    Bean                           |Chausia                            |43
    Coca-Cola                      |Chausia                            |61
    Butter                         |Chausia                            |42
    Bean                           |Ranucia                            |4
    Coca-Cola                      |Ranucia                            |23
    Butter                         |Ranucia                            |74
    Bean                           |Rodunia                            |66
    Coca-Cola                      |Rodunia                            |20
    Butter                         |Rodunia                            |26
    Bean                           |Serisia                            |2
    Coca-Cola                      |Serisia                            |35
    Butter                         |Serisia                            |16

表2

  Product                           |Supermarket                            |Price
------------------------------------+---------------------------------------+---------
    Bean                            |Adonis                                 |87
    Bean                            |Rodunia                                |66
    Bean                            |Emynia                                 |43
    Butter                          |Ranucia                                |74
    Butter                          |Adonis                                 |60
    Butter                          |Emynia                                 |50
    Coca-Cola                       |Emynia                                 |84
    Coca-Cola                       |Chausia                                |61
    Coca-Cola                       |Serisia                                |35

1 个答案:

答案 0 :(得分:2)

试试这个:

SELECT "Product", "Supermarket", "Price"
FROM (
  SELECT "Product", "Supermarket", "Price", rank() OVER (
      PARTITION BY "Product" ORDER BY "Price" DESC
      ) AS rank
  FROM Table1
  ) t
WHERE rank <= 3

sqlfiddle demo

由于您在Bean的前三名中有两个具有相同价格的产品,这将为您提供该产品的4个结果:

PRODUCT SUPERMARKET     PRICE
Bean        Adonis       87
Bean        Rodunia      66
Bean        Chausia      43
Bean        Emynia       43
Butter      Ranucia      74
Butter      Adonis       60
Butter      Emynia       50
Coca-Cola   Emynia       84
Coca-Cola   Chausia      61
Coca-Cola   Serisia      35

Docs了解有关rank()

的更多信息