Date problem in Postgresql

Asked

Viewed 78 times

1

I have this query below and I want faturamento, ticket_medio and qtd_pecas, qtd_atendimentos, be divided by the numbers of Tuesdays. Example had 5 Tuesday fairs with registered values in this view, I want it to be divided by 5.

Which postgres function can I use?

SELECT
    id_empresa,
    id_loja,
    sum(faturamento),
    sum(ticket_medio),
    sum(qtd_pecas),
    sum(qtd_atendimentos)
FROM
    sumario_diario
WHERE
    to_char(data_sumario, 'dy') = 'tue'
    AND id_loja = 19
GROUP BY
    id_empresa,
    id_loja;
  • 1

    Could you attach an example of how this data is stored and how would you like it returned in SQL? I believe it would be clearer for the community staff to help.

  • @Camilosantos I answered the question and created an example, see here: my answer

2 answers

0


To be able to find the answer:

SELECT  id_empresa, id_loja, avg(faturamento), avg(ticket_medio), avg(qtd_pecas),avg(qtd_atendimentos) FROM sumario_diario

Where TO_CHAR(data_sumario, 'dy') = 'Tue' and id_loja = 19 group by id_empresa, id_loja

0

See if this resolves

SELECT
    id_empresa,
    id_loja,
    sum(faturamento),
    sum(ticket_medio),
    sum(qtd_pecas),
    sum(qtd_atendimentos),

    avg(faturamento) as fat_terca,
    avg(ticket_medio) as ticket_terca,
    avg(qtd_pecas) as pecas_terca,
    avg(qtd_atendimentos) as aten_terca

FROM
    sumario_diario
WHERE
    to_char(data_sumario, 'dy') = 'tue'
    AND id_loja = 19
GROUP BY
    id_empresa,
    id_loja;

Click here to run the Test!

  • @Camilosantos here my answer with example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.