alwaystrying
alwaystrying

Reputation: 163

Creating a select to 'de-normalise' the data within a table

I put the work de-normalise in quote marks, because it might not be the right way of putting it, but not too sure how else to describe it...

I have the following table

Source   Priority   Attribute
A        1          Name
B        2          Name
C        3          Name
A        1          Address
B        2          Address
C        3          Address
A        2          Email
B        3          Email
C        1          Email

I would like my select to return:

Source Name_Pri  Addr_Pri  Email_Pri
A      1         1         2
B      2         2         3
C      3         3         1     

Thanks

Upvotes: 0

Views: 26

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270483

You are looking for a pivot. I often do this using conditional aggregation:

select source,
       max(case when attribute = 'Name' then priority end) as name_priority,
       max(case when attribute = 'Address' then priority end) as address_priority,
       max(case when attribute = 'Email' then priority end) as email_priority
from t
group by source;

Upvotes: 2

Related Questions