Postgres Complex在视图中选择

时间:2013-02-20 15:48:46

标签: postgresql select view

我有一个完美的选择条款:

SELECT 
    "Aspectos"."ID" AS "Aspecto Normativo ID", 
    "Aspectos"."Aspecto" AS "Aspecto Normativo",        
    "Fatores"."ID", "Fatores"."Fator" AS "Fator Normativo",
    "Diagnostico"."Vinculo_Final", 
    "Fatores_1"."ID", 
    "Fatores_1"."Fator" AS "Fator Determinativo", 
    "Aspectos_1"."ID" AS "Aspecto Determinativo ID", 
    "Aspectos_1"."Aspecto" AS "Aspecto Determinativo", 
    Count("Itens"."ID") AS "No Itens", 
    Count("Itens"."ID") AS "Pri"
FROM "Diagnostico" INNER JOIN ("Aspectos" AS "Aspectos_1" 
INNER JOIN (("Fontes" INNER JOIN "Itens" ON "Fontes"."ID" = "Itens"."Fonte") 
INNER JOIN ("Fatores" AS "Fatores_1" 
INNER JOIN ("Aspectos" 
INNER JOIN ("Vinculos" 
INNER JOIN "Fatores" 
ON "Vinculos"."Fator_Normativo" = "Fatores"."ID") 
ON ("Aspectos"."ID" = "Fatores"."Aspecto") 
AND ("Aspectos"."ID" = "Fatores"."Aspecto")) 
ON "Fatores_1"."ID" = "Vinculos"."Fator_Determinativo") 
ON "Itens"."ID" = "Vinculos"."Item") 
ON "Aspectos_1"."ID" = "Fatores_1"."Aspecto") 
ON "Diagnostico"."ID" = "Vinculos"."Diagnostico_ID"
GROUP BY "Aspectos"."ID", "Aspectos"."Aspecto", 
          "Fatores"."ID", "Fatores"."Fator", 
          "Diagnostico"."Vinculo_Final", 
          "Fatores_1"."ID", 
          "Fatores_1"."Fator", 
          "Aspectos_1"."ID", 
          "Aspectos_1"."Aspecto"
ORDER BY  "Aspectos"."ID", "Aspectos_1"."ID", 
         "Fatores"."Fator", "Fatores_1"."Fator";

但是当我尝试使用相同的选择创建视图时,我遇到了错误:

ERROR: column "ID" specified more than one time

任何人都可以帮助我。 感谢

1 个答案:

答案 0 :(得分:2)

您有"Fatores"."ID"(第4行)和"Fatores_1"."ID"(第6行)。给他们不同的别名。

对于此类复杂查询,建议在语句中每行只有一(1)列,以便更好地查看。此外,建议始终为列提供别名。