Search code examples
sqlsql-serverpivotrow-numberranking-functions

Pivot and also get ranking


I am trying to write a SQL that can pivot and also get its ranking order all in one line. Not sure how.

Example:

Pivot the area but also get the ranking of the cost (order of most expensive)

table name: aaa

entity_id | entity_type | datetime   | area | cost
----------+-------------+------+-----+------+-----
123       | ITEM        | 2020-01-01 | US   | 8
123       | ITEM        | 2020-01-01 | AP   | 7
123       | ITEM        | 2020-01-01 | EU   | 6
123       | ITEM        | 2020-01-02 | US   | 1
123       | ITEM        | 2020-01-02 | AP   | 2
123       | ITEM        | 2020-01-02 | EU   | 3
888       | BOX         | 2020-01-01 | US   | 8
888       | BOX         | 2020-01-01 | AP   | 7
888       | BOX         | 2020-01-01 | EU   | 6
888       | BOX         | 2020-01-02 | US   | 1
888       | BOX         | 2020-01-02 | AP   | 2
888       | BOX         | 2020-01-02 | EU   | 3

What I want is:

entity_id | entity_type | datetime   | US | AP | EU | US_rank | AP_rank | EU_rank
----------+-------------+------------+----+----+----+---------+---------+---------
123       | ITEM        | 2020-01-01 | 8  | 7  | 6  | 1       | 2       | 3
123       | ITEM        | 2020-01-02 | 8  | 7  | 6  | 3       | 2       | 1
888       | BOX         | 2020-01-01 | 8  | 7  | 6  | 1       | 2       | 3
888       | BOX         | 2020-01-02 | 8  | 7  | 6  | 3       | 2       | 1

Currently, I only know how to pivot. Not sure how to get the 'ranking' part

SELECT  
  p.entity_id, p.entity_type, p.[datetime], p.[US], p.[AP], p.[EU]
FROM
  aaa as a
PIVOT
(
  MAX(cost)
  FOR area IN ([US], [AP], [EU])
) p

(which returns)

entity_id | entity_type | datetime   | US | AP | EU 
----------+-------------+------------+----+----+----
123       | ITEM        | 2020-01-01 | 8  | 7  | 6  
123       | ITEM        | 2020-01-02 | 8  | 7  | 6  
888       | BOX         | 2020-01-01 | 8  | 7  | 6  
888       | BOX         | 2020-01-02 | 8  | 7  | 6  

I assume I need to add this somewhere before I pivot, but not sure what to do after that

ranking = ROW_NUMBER() OVER(PARTITION BY entity_id, entity_type, [datetime] ORDER BY cost DESC)

(I don't mind using RANK or ROW_NUMBER for this problem)


Solution

  • Use conditional aggregation:

    select entity_id, entity_type, datetime,
           max(case when area = 'US' then cost end) as us,
           max(case when area = 'AP' then cost end) as ap,
           max(case when area = 'EU' then cost end) as eu,
           max(case when area = 'US' then rnk end) as us_rank,
           max(case when area = 'AP' then rnk end) as ap_rank,
           max(case when area = 'EU' then rnk end) as eu_rank
    from (select s.*,
                 rank() over (partition by entity_id, entity_type, datetime 
                              order by cost desc
                             ) as rnk
          from aaa a
         ) a
    group by entity_id, entity_type, datetime