2010-03-19 3 views
6

Sto provando a creare una vista tipo tabella pivot in postgresql e ci sono quasi! Ecco la domanda di base:modo corretto per creare una tabella pivot in postgresql utilizzando CASO QUANDO

select 
acc2tax_node.acc, tax_node.name, tax_node.rank 
from 
tax_node, acc2tax_node 
where 
tax_node.taxid=acc2tax_node.taxid and acc2tax_node.acc='AJ012531'; 

E i dati:

acc |   name   |  rank  
----------+-------------------------+-------------- 
AJ012531 | Paromalostomum fusculum | species 
AJ012531 | Paromalostomum   | genus 
AJ012531 | Macrostomidae   | family 
AJ012531 | Macrostomida   | order 
AJ012531 | Macrostomorpha   | no rank 
AJ012531 | Turbellaria    | class 
AJ012531 | Platyhelminthes   | phylum 
AJ012531 | Acoelomata    | no rank 
AJ012531 | Bilateria    | no rank 
AJ012531 | Eumetazoa    | no rank 
AJ012531 | Metazoa     | kingdom 
AJ012531 | Fungi/Metazoa group  | no rank 
AJ012531 | Eukaryota    | superkingdom 
AJ012531 | cellular organisms  | no rank 

Quello che sto cercando di ottenere è la seguente:

acc  | species     | phylum 
AJ012531 | Paromalostomum fusculum | Platyhelminthes 

che sto cercando di fare questo con CASO QUANDO , così ho ottenuto quanto segue:

select 
acc2tax_node.acc, 
CASE tax_node.rank WHEN 'species' THEN tax_node.name ELSE NULL END as species, 
CASE tax_node.rank WHEN 'phylum' THEN tax_node.name ELSE NULL END as phylum 
from 
tax_node, acc2tax_node 
where 
tax_node.taxid=acc2tax_node.taxid and acc2tax_node.acc='AJ012531'; 

che mi dà l'output:

acc |   species   |  phylum  
----------+-------------------------+----------------- 
AJ012531 | Paromalostomum fusculum | 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | Platyhelminthes 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | 
AJ012531 |       | 

Ora so che devo gruppo con acc ad un certo punto, in modo da provare

select 
acc2tax_node.acc, 
CASE tax_node.rank WHEN 'species' THEN tax_node.name ELSE NULL END as sp, 
CASE tax_node.rank WHEN 'phylum' THEN tax_node.name ELSE NULL END as ph 
from 
tax_node, acc2tax_node 
where 
tax_node.taxid=acc2tax_node.taxid and acc2tax_node.acc='AJ012531' 
group by acc2tax_node.acc; 

ma ho la temuta

ERROR: column "tax_node.rank" must appear in the GROUP BY clause or be used in an aggregate function 

Tutti gli esempi precedenti che sono stato in grado di trovare utilizzano qualcosa come SUM() attorno alle istruzioni CASE, quindi suppongo che sia la funzione di aggregazione. Ho provato con FIRST():

select 
acc2tax_node.acc, 
FIRST(CASE tax_node.rank WHEN 'species' THEN tax_node.name ELSE NULL END) as sp, 
FIRST(CASE tax_node.rank WHEN 'phylum' THEN tax_node.name ELSE NULL END) as ph 
from tax_node, acc2tax_node where tax_node.taxid=acc2tax_node.taxid and acc2tax_node.acc='AJ012531' group by acc2tax_node.acc; 

ma ottenere l'errore:

ERROR: function first(character varying) does not exist 

Qualcuno può offrire eventuali suggerimenti?

+0

La prego di inviare i risultati di questa query: ' SELECT * FROM acc2tax_node WHERE acc = 'AJ012531''? – Quassnoi

risposta

5

Utilizzare MAX() o MIN(), non PRIMO(). In questo scenario, avrai tutti i valori NULL nella colonna per ciascun valore di gruppo ad eccezione, al massimo, di uno con un valore non nullo. Per definizione, questo è sia il MIN che il MAX di quel set di valori (tutti i valori nulli sono esclusi).

+0

Funziona perfettamente, grazie. Per qualche ragione ho pensato che MAX() non avrebbe funzionato perché stavo usando valori stringa. – mojones

0
SELECT atn.acc, ts.name AS species, tp.name AS phylum 
FROM acc2tax_node atn 
LEFT JOIN 
     tax_node ts 
ON  ts.taxid = atn.taxid 
     AND ts.rank = 'species' 
LEFT JOIN 
     tax_node tp 
ON  tp.taxid = atn.taxid 
     AND tp.rank = 'phylum' 
WHERE atn.acc = 'AJ012531 ' 
+0

Si prega di scusare i miei terribili tentativi di formattazione :-) – mojones

0

Ulteriori informazioni come richiesto (in una risposta piuttosto che un commento per la formattazione bello):

SELECT * FROM acc2tax_node WHERE acc = 'AJ012531'; 

    acc | taxid 
----------+-------- 
AJ012531 | 66400 
AJ012531 | 66399 
AJ012531 | 39216 
AJ012531 | 39215 
AJ012531 | 166235 
AJ012531 | 166384 
AJ012531 | 6157 
AJ012531 | 33214 
AJ012531 | 33213 
AJ012531 | 6072 
AJ012531 | 33208 
AJ012531 | 33154 
AJ012531 | 2759 
AJ012531 | 131567 
2

PostgreSQL ha un paio di funzioni per le query pivot, si veda questo articolo in Postgresonline. È possibile trovare queste funzioni nel contrib.

+0

Sì, ho il sospetto che il modo corretto per farlo è con scrosstab. Mi piacerebbe ancora capire cosa sto facendo di sbagliato qui per la mia educazione. – mojones

0

Esegui:

SELECT report.* FROM crosstab(
select 
acc2tax_node.acc, tax_node.name, tax_node.rank 
from 
tax_node, acc2tax_node 
where 
tax_node.taxid=acc2tax_node.taxid and acc2tax_node.acc='AJ012531'; 
) AS report(species text, enus text, family text, ...) 
0

Come Matthew Wood ha sottolineato, utilizzare MIN() o MAX(), non FIRST():

SELECT 
    an.acc, 
    MAX(
     CASE tn.rank 
      WHEN 'species' THEN tn.name 
      ELSE NULL 
     END 
    ) AS species, 
    MAX(
     CASE tn.rank 
      WHEN 'phylum' THEN tn.name 
      ELSE NULL 
     END 
    ) AS phylum 
FROM tax_node tn, 
    acc2tax_node an 
WHERE tn.taxid = an.taxid 
    and an.acc = 'AJ012531' 
GROUP by an.acc;