1
I’m working with the postgresql
, running the following query :
SELECT "tcfac"."co_cds_ficha_ativ_col",
to_char(dt_ativ_col, 'DD/MM/YYYY') AS "data_atividade",
to_char(hr_inicio, 'HH24:MI') AS "hora_inicio",
to_char(hr_fim, 'HH24:MI') AS "hora_fim",
"tcfac"."cod_equipe_ine",
"tcfac"."qt_participante_ativ",
"tcfac"."uni_codigo",
"usr"."usr_nome",
"uni"."uni_desc",
"tctac"."no_cds_tipo_ativ_col"
FROM "tb_cds_ficha_ativ_col" AS "tcfac"
INNER JOIN "usuarios" AS "usr" ON tcfac.usr_codigo = usr.usr_codigo
INNER JOIN "unidade" AS "uni" ON tcfac.uni_codigo = uni.uni_codigo
INNER JOIN "tb_cds_tipo_ativ_col" AS "tctac" ON tcfac.tp_cds_ativ_col = tctac.co_cds_tipo_ativ_col
WHERE (tcfac.dt_ativ_col BETWEEN to_timestamp('05/08/2019', 'DD/MM/YYYY') AND to_timestamp('05/08/2019', 'DD/MM/YYYY'))
ORDER BY "tcfac"."uni_codigo" ASC
That returns me the following result:
My question is, would it be possible to add equal amounts of uni_code ? I will use that in a report. For example, two of these 4 queries are from the same unit. I believe bringing this directly from the database is better than dealing with it using PHP.
I made some changes in the Query after some comments but the result came out a bit confusing .
She didn’t count the Count’s just informed a:
SELECT "tcfac"."co_cds_ficha_ativ_col",
to_char(dt_ativ_col, 'DD/MM/YYYY') AS "data_atividade",
to_char(hr_inicio, 'HH24:MI') AS "hora_inicio",
to_char(hr_fim, 'HH24:MI') AS "hora_fim",
"tcfac"."cod_equipe_ine",
"tcfac"."qt_participante_ativ",
"tcfac"."uni_codigo",
"usr"."usr_nome",
"uni"."uni_desc",
"tctac"."no_cds_tipo_ativ_col"
FROM "tb_cds_ficha_ativ_col" AS "tcfac"
INNER JOIN "usuarios" AS "usr" ON tcfac.usr_codigo = usr.usr_codigo
INNER JOIN "unidade" AS "uni" ON tcfac.uni_codigo = uni.uni_codigo
INNER JOIN "tb_cds_tipo_ativ_col" AS "tctac" ON tcfac.tp_cds_ativ_col = tctac.co_cds_tipo_ativ_col
WHERE (tcfac.dt_ativ_col BETWEEN to_timestamp('05/08/2019', 'DD/MM/YYYY') AND to_timestamp('05/08/2019', 'DD/MM/YYYY'))
ORDER BY "tcfac"."uni_codigo" ASC
You can use the
GROUP BY
, but why these 4 lines would be of the same unit being that two haveuni_codigo
equal to 505 and the other two equal to 506?– Woss
Use the COUNT aggregation function together with the GROUP BY clause. If you only want those who have duplicity then also use the HAVING clause.
– anonimo
Anderson Carlos Woss, so really are pairs had not attacked me to it. The group by does not work only when one Row is identical to another ?
– Lucas Alves
Hello @Lucasalves, the answer I prepared solves your question?
– RXSD