Bonjour,

je possède une table "table1" avec 4 colonnes :
codemaille, habitats, surface, the_geom

---------------------------------------------
codemaille | habitats| surface| the_geom|
---------------------------------------------
1 | "1" | 8 | xxx
1 | "2" | 1 | xxx
2 | "3" | 8 | yyy
2 | "13" | 1 | yyy
2 | "3" | 8 | yyy
3 | "14" | 1 | zzz
3 | "3" | 7 | zzz
3 | "4" | 1 | zzz
...
je souhaite regrouper la table par le codemaille selon la condition suivante :
prendre l'habitats et sa surface correspondante si l'habitats='13' ou '14' sinon je prends l'habitats correspond à la surface max (habitat majoritaire).

pour faire j'ai fais cette requête :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
 
SELECT distinct h.codemaille, max(h.habitats) as codehabitat
FROM "Table1" as h, (SELECT t.codemaille as codemaille,
case   when t.habitats='13' then t.surface
        when t.habitats='14' then t.surface
        else max(t.surface)
END
		   FROM "Table1" as t
		   GROUP BY t.codemaille)AS hm 
WHERE h.codemaille=hm.codemaille AND h.surface=hm.surface 
GROUP BY h.codemaille
;
évidement Postgres envoie l'erreur : « t.habitats » doit apparaître dans la clause GROUP BY ou doit être utilisé dans une fonction d'agrégat.

Comment faire ???
Merci