My layer has 82431 points. Each point has an attribute var
with a single value a, or b or c,...
I snap points, group by geometry and count the number depending the var attribute.
So I start with :
geom | var
pt a
pt b
pt1 a
pt1 a
pt1 b
pt2 c
...
And want :
geom count(a) count(b) count(c)
pt 1 1 0
pt1 2 1 0
pt2 8 0 1
...
In QGIS 3.14, with a shapefile, this query gives through a virtual layer the correct result so to speak 157 rows (1 per geometry) and the columns with count :
select st_snaptogrid(m.geometry, 25) as geom,
count( case when var Like 'a' THEN 1 END),
count( case when var Like 'b' THEN 1 END ),
count( case when var Like 'c' THEN 1 END )
from mytbl m
group by geom
Now, I uploaded the shapefile into PostgreSQL 11.5. The same query (geom instead of geometry though) does NOT group geometries. I have 64967 rows with some weird values in the columns between 0 and 10... Obviously, I do something wrong but where ?
gid id_fiche var geom
1 "74760" "Lithique" "POINT(566780.44 6913793.64)"
2 "74763" "Lithique" "POINT(566780.1 6913793.78)"
3 "74764" "Lithique" "POINT(566780.2 6913793.46)"
4 "74765" "Céramique" "POINT(566780.43 6913793.45)"
5 "74766" "Céramique" "POINT(566780.11 6913793.41)"
6 "74767" "Lithique" "POINT(566780.18 6913793.28)"
7 "76359" "Faune" "POINT(566780.22 6913793.79)"
8 "76360" "Lithique" "POINT(566780.19 6913793.73)"
9 "76361" "Lithique" "POINT(566780.14 6913793.3)"
10 "74768" "Lithique" "POINT(566780.13 6913793.09)"
11 "74769" "Lithique" "POINT(566780.43 6913793.03)"
12 "76362" "Lithique" "POINT(566780.36 6913793.16)"
13 "76363" "Lithique" "POINT(566780 6913792.98)"
14 "76364" "Céramique" "POINT(566780.3 6913792.94)"
15 "76365" "Lithique" "POINT(566780.28 6913792.86)"
16 "76366" "Lithique" "POINT(566780.37 6913792.73)"
17 "40700" "Céramique" "POINT(566781.51 6913795.88)"
18 "74759" "Lithique" "POINT(566780.71 6913793.59)"
19 "74761" "Lithique" "POINT(566780.5 6913794.14)"
20 "74762" "Lithique" "POINT(566780.2 6913794.17)"
21 "76358" "Céramique" "POINT(566780.21 6913794.14)"
22 "76385" "Céramique" "POINT(566781.64 6913795.66)"
23 "76386" "Céramique" "POINT(566781.84 6913795.43)"
24 "76387" "Faune" "POINT(566781.73 6913795.3)"
25 "76388" "Lithique" "POINT(566781.7 6913794.22)"
26 "76389" "Lithique" "POINT(566781.67 6913794.18)"
27 "76390" "Céramique" "POINT(566781.98 6913795.05)"
28 "76391" "Céramique" "POINT(566782.18 6913794.2)"
29 "76393" "Lithique" "POINT(566781.77 6913794.73)"
30 "76394" "Lithique" "POINT(566781.84 6913795.78)"
31 "76395" "Lithique" "POINT(566781.95 6913793.64)"
32 "76397" "Lithique" "POINT(566782.26 6913793.48)"
33 "76402" "Céramique" "POINT(566781.88 6913793.38)"
34 "108456" "Lithique" "POINT(566782.69 6913796.39)"
35 "108457" "Lithique" "POINT(566782.74 6913796.6)"
36 "108458" "Céramique" "POINT(566782.67 6913796.65)"
37 "108459" "Céramique" "POINT(566782.53 6913796.65)"
38 "42136" "Céramique" "POINT(566780.13 6913789.78)"
39 "74770" "Lithique" "POINT(566780.49 6913792.82)"
40 "74771" "Lithique" "POINT(566781.05 6913793.11)"
41 "74772" "Lithique" "POINT(566780.75 6913792.53)"
42 "74773" "Lithique" "POINT(566780.41 6913792.2)"
43 "74774" "Lithique" "POINT(566780.97 6913792.03)"
44 "74775" "Lithique" "POINT(566781.03 6913791.8)"
45 "74776" "Lithique" "POINT(566780.62 6913791.63)"
46 "74777" "Céramique" "POINT(566781.04 6913790.59)"
47 "76367" "Lithique" "POINT(566780.57 6913792.51)"
48 "76368" "Faune" "POINT(566781.11 6913792.42)"
49 "76369" "Lithique" "POINT(566780.85 6913792.63)"
50 "76370" "Céramique" "POINT(566780.91 6913792.81)"
SELECT t1.geom2, count(case when t1.var Like 'a' Then 1 END), ... FROM (SELECT st_astext(st_snaptogrid(m.geometry, 25)) as geom2, var FROM mytbl m) t1 GROUP BY t1.geom2
?mytbl
for testing ?