Oracle “Partition By” Keyword

前端 未结 6 894
不思量自难忘°
不思量自难忘° 2020-11-29 14:22

Can someone please explain what the partition by keyword does and give a simple example of it in action, as well as why one would want to use it? I have a SQL

6条回答
  •  臣服心动
    2020-11-29 15:10

    The concept is very well explained by the accepted answer, but I find that the more example one sees, the better it sinks in. Here's an incremental example:

    1) Boss says "get me number of items we have in stock grouped by brand"

    You say: "no problem"

    SELECT 
          BRAND
          ,COUNT(ITEM_ID) 
    FROM 
          ITEMS
    GROUP BY 
          BRAND;
    

    Result:

    +--------------+---------------+
    |  Brand       |   Count       | 
    +--------------+---------------+
    | H&M          |     50        |
    +--------------+---------------+
    | Hugo Boss    |     100       |
    +--------------+---------------+
    | No brand     |     22        |
    +--------------+---------------+
    

    2) The boss says "Now get me a list of all items, with their brand AND number of items that the respective brand has"

    You may try:

     SELECT 
          ITEM_NR
          ,BRAND
          ,COUNT(ITEM_ID) 
     FROM 
          ITEMS
     GROUP BY 
          BRAND;
    

    But you get:

    ORA-00979: not a GROUP BY expression 
    

    This is where the OVER (PARTITION BY BRAND) comes in:

     SELECT 
          ITEM_NR
          ,BRAND
          ,COUNT(ITEM_ID) OVER (PARTITION BY BRAND) 
     FROM 
          ITEMS;
    

    Whic means:

    • COUNT(ITEM_ID) - get the number of items
    • OVER - Over the set of rows
    • (PARTITION BY BRAND) - that have the same brand

    And the result is:

    +--------------+---------------+----------+
    |  Items       |  Brand        | Count()  |
    +--------------+---------------+----------+
    |  Item 1      |  Hugo Boss    |   100    | 
    +--------------+---------------+----------+
    |  Item 2      |  Hugo Boss    |   100    | 
    +--------------+---------------+----------+
    |  Item 3      |  No brand     |   22     | 
    +--------------+---------------+----------+
    |  Item 4      |  No brand     |   22     | 
    +--------------+---------------+----------+
    |  Item 5      |  H&M          |   50     | 
    +--------------+---------------+----------+
    

    etc...

提交回复
热议问题