Question

Data Source: http://www-01.sil.org/iso639-3/iso-639-3_20130520.tab

So I have a table like this (abbreviated from the actual table):

Part3   Part2B  Part2T  Part1   Scope   Type    Name    Comment 
aaa     NULL    NULL    NULL    I       L       Ghotuo  NULL    
aar     aar     aar     aa      I       L       Afar    NULL    
ach     ach     ach     NULL    I       L       Acoli   NULL    
ave     ave     ave     ae      I       A       Avestan NULL    
bod     tib     bod     bo      I       L       Tibetan NULL    
ces     cze     ces     cs      I       L       Czech   NULL    

The columns Id, Part2B, Part2T, and Part1 are codes used in various standards internationally and are all part of ISO 639. While this is nice for looking up equivalent codes from ISO 639-2(B), ISO 639-2(T), or ISO 639-1 by ISO 639-3, it is not so useful for reverse look-ups when you don't know which column the codes belongs to. I would prefer to have a view structured like this (created from the above table with FALSE values simply left blank for clarity):

Code    Part3   Part2B  Part2T  Part1   
aa                              TRUE    
aaa     TRUE                            
aar     TRUE    TRUE    TRUE            
ach     TRUE    TRUE    TRUE            
ae                              TRUE    
ave     TRUE    TRUE    TRUE            
bo                              TRUE    
bod     TRUE            TRUE            
ces     TRUE            TRUE            
cs                              TRUE    
cze             TRUE                    
tib             TRUE                    

This structure makes it clear that Part2T is a subset of Part3, but Part2B is not. Hence, a single column of all codes is needed with boolean *columns* to specify which part of the ISO 639 standard the code is valid under. The goal is also to not duplicate data or create too many tables to maintain if a view can be used to transform the data into the structure I want.


How can I create this view from the table, combining the multiple columns into 1 unique column? …or is it even possible with PostgreSQL?

Was it helpful?

Solution

Please use this: http://sqlfiddle.com/#!2/bcc34/12

SELECT DISTINCT PARTS.PART,
CASE WHEN PARTS.PART=T.PART3 THEN 'TRUE' ELSE NULL END AS PART3,
CASE WHEN PARTS.PART=T.PART2B THEN 'TRUE' ELSE NULL END AS PART2B,
CASE WHEN PARTS.PART=T.PART2T THEN 'TRUE' ELSE NULL END AS PART2T,
CASE WHEN PARTS.PART=T.PART1 THEN 'TRUE' ELSE NULL END AS PART1
 FROM 
(
SELECT
Part3,   
 Part2B, 
 Part2T, 
 Part1,  
 Scope,  
 Type,  
 Name,  
 Comment
FROM 
TEST_ISO ) T
JOIN 
(
SELECT PART3  AS PART FROM TEST_ISO
UNION
SELECT PART2B FROM TEST_ISO
UNION 
SELECT PART2T FROM TEST_ISO
UNION
SELECT PART1 FROM TEST_ISO) PARTS
ON T.PART3=PARTS.PART
OR T.PART2B=PARTS.PART
OR T.PART2T=PARTS.PART
OR T.PART1=PARTS.PART

If you do not want the NULL you can change the ELSE NULL as ELSE ''

Here is my result:

PART    PART3   PART2B  PART2T  PART1
aaa     TRUE    (null)  (null)  (null)
aar     TRUE    TRUE    TRUE    (null)
ach     TRUE    TRUE    TRUE    (null)
ave     TRUE    TRUE    TRUE    (null)
bod     TRUE    (null)  TRUE    (null)
ces     TRUE    (null)  TRUE    (null)
tib     (null)  TRUE    (null)  (null)
cze     (null)  TRUE    (null)  (null)
aa      (null)  (null)  (null)  TRUE
ae      (null)  (null)  (null)  TRUE
bo      (null)  (null)  (null)  TRUE
cs      (null)  (null)  (null)  TRUE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top