Count duplicate numbers in postgres

时间:2017-12-08 15:36:43

标签: count postgresql-9.1

i have quick question.

I would like to count rows with same number. This is what i get:

enter image description here

So i have 4 rows, with same number(59 b) and in count column i get ones. I need to get 4 instead of 1, becouse i have 4 rows with same number.

This is my sql:

SELECT (z.miasto ||' '|| z.ulica ||' '|| z.nr_dom) as adres, (z.sym ||z.sym_ul||' '|| z.nr_dom) as symbol, zd.status, zd.id_administrator, z.uwagi,
COUNT(*) as liczba_osob
FROM zgloszenia as z
LEFT JOIN zgl_dzialamy AS zd ON (z.sym ||z.sym_ul||' '|| z.nr_dom) = (zd.sym ||zd.sym_ul||' '|| zd.nr_budynku)
WHERE status < 5
group by adres, symbol, zd.id_administrator, z.uwagi, zd.status

Can somebody tell me what am I doing wrong?

1 个答案:

答案 0 :(得分:0)

COUNT无效的原因是因为分组值无法聚合。 它们似乎彼此不同(可能是黑色的值:z.uwagi)。

您可以做的是一起选择非DISTINCT值并改为COUNT。 COUNTing完成后,您可以将此VALUE加入到您的表中。或者,您可以将此列作为数组或json对象聚合到表中。

SELECT 
  (z.miasto ||' '|| z.ulica ||' '|| z.nr_dom) as adres,
  (z.sym ||z.sym_ul||' '|| z.nr_dom) as symbol,
  zd.status,
  zd.id_administrator,
  -- z.uwagi, -- VALUE IS DISTINCT
  COUNT(*) as liczba_osob
  FROM zgloszenia as z
    LEFT JOIN zgl_dzialamy AS zd ON (z.sym ||z.sym_ul||' '|| z.nr_dom) = (zd.sym ||zd.sym_ul||' '|| zd.nr_budynku)
  WHERE status < 5
  group by
    adres,
    symbol,
    zd.id_administrator,
    -- z.uwagi,
    zd.status