2017-08-01 26 views
0

我把工作去標準化爲引號,因爲它可能不是正確的放置方式,但不太確定如何描述它。 ..創建一個select來「去規格化」表中的數據

我有以下表

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 

我想我的選擇返回:

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

感謝

回答

2

您正在尋找一個支點。我經常使用條件聚合來做到這一點:

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; 
+0

完美!這有工作。非常感謝!知道這將是一個簡單的... – alwaystrying

相關問題