Format data from the same person in array. POSTGRES

Asked

Viewed 25 times

0

I have an API that returns values to be plotted on a given graph Dashboard. However, this data needs to be formatted to fit the syntax of Charts.js.

The consultation I’m doing is as follows:

SELECT
    Vendedores.nome,
    Tempo.mes,
    sum (Venda.qtd_vendas)
FROM dw.tb_vendas as Venda
LEFT JOIN dw.tb_tempo as Tempo ON Tempo.data_numeric = Venda.tempo_data
LEFT JOIN dw.tb_vendedores as Vendedores ON Vendedores.id = Venda.vendedor_id
GROUP BY 1, 2
ORDER BY 1;

It returns the data this way:

{
    "label": "PESSOA 1640",
    "labels": "Julho",
    "data": "90"
  },
  {
    "label": "PESSOA 1640",
    "labels": "Marco",
    "data": "80"
  },

...

}

I need them to be returned this way:

{
    "label": "PESSOA 1640",
    "labels": ["Marco", "Julho", "Agosto"]
    "data": ["80", "90", "480"]
},

Remembering that there are several people. For each of them I want to join the data in arrays. In case I’m adding up the sales of a salesman during the month. Only for each month the seller will repeat itself, what I want is for the seller to appear only once, and the sum of each month to be allocated in one array.


So, remembering that there are several people. For each of them I want to join the data in arrays. In case I’m adding up the sales of a salesman during the month. Only for each month the seller will repeat itself, what I want is for the seller to appear only once, and the sum of each month to be allocated in an array.

  • Search by aggregation function array_agg.

No answers

Browser other questions tagged

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