Pourquoi ma requête n'aplatit-elle pas les données comme prévu ?
J'interroge une table où les colonnes contiennent des tableaux. Mon objectif est de démêler les éléments des tableaux pour trouver des éléments uniques et de les transformer en lignes.
SELECT
table1.tag_names,
table1.tag_ids,
rank_position
FROM table1
CROSS JOIN UNNEST (tag_ids, tag_names)
WITH ORDINALITY as T (tag_ids, tag_names, rank_position)
ORDER BY tag_ids
Résultats:
| tag_names | tag_ids | rang_position |
|---|---|---|
| ["rouge", "bleu", "vert"] | [111, 222, 333] | une |
| ["rouge", "bleu", "jaune"] | [111, 222, 444] | 4 |
Solution du problème
Vous devez utiliser l'alias introduit pour les données aplaties CROSS JOIN UNNESTdans le select :
-- sample data
WITH dataset (tag_names, tag_ids) AS (
VALUES (array['red', 'blue', 'green'], array[111, 222, 444])
)
-- query
select T.tag_names,
T.tag_ids,
rank_position
from dataset
CROSS JOIN UNNEST (tag_ids, tag_names)
WITH ORDINALITY as T (tag_ids, tag_names, rank_position)
ORDER BY tag_ids
Production:
| tag_names | tag_ids | rang_position |
|---|---|---|
| rouge | 111 | une |
| bleu | 222 | 2 |
| vert | 444 | 3 |
Aucun commentaire:
Enregistrer un commentaire