SQLITE query GROUP BY with SUM Using Delphi and Zeos

227 views Asked by At

I have an app created using Delphi XE3 accessing the SQLite database with Zeos Component.

A select command in SQLite that return 3 rows:

 select p.descricao, pg.valor
from pagamento pg
inner join venda v on pg.origem = 'venda' and v.id_venda = pg.id_origem
left join pagamento_tipo p on pg.id_pagamento_tipo = p.id_pagamento_tipo
where v.data >= '2021-01-19' and v.data <= '2021-01-19'
and v.ra_status_venda in ( 'Finalizado', 'Pedido')

enter image description here

but when I put the group command the information result is wrong.

    select p.descricao, sum(pg.valor) as valor
from pagamento pg
inner join venda v on pg.origem = 'venda' and v.id_venda = pg.id_origem
left join pagamento_tipo p on pg.id_pagamento_tipo = p.id_pagamento_tipo
where v.data >= '2021-01-19' and v.data <= '2021-01-19'
and v.ra_status_venda in ( 'Finalizado', 'Pedido')
group by descricao

enter image description here

the P02 not sum.

and, if I sum all row, without group by, the value was correct too.

select sum(pg.valor) as valor
from pagamento pg
inner join venda v on pg.origem = 'venda' and v.id_venda = pg.id_origem
left join pagamento_tipo p on pg.id_pagamento_tipo = p.id_pagamento_tipo
where v.data >= '2021-01-19' and v.data <= '2021-01-19'
and v.ra_status_venda in ( 'Finalizado', 'Pedido')

enter image description here

PS: in another period this commands sum correctly. PS2 I'm using dll 32bits for windows.

1

There are 1 answers

10
Márcio Rossato On

With tips @Shawn and @forpas I solve this problem, the SQLite type of field NUMERIC converts the numbers to other types in the RUNTIME select command, how the two first values were INTEGER and the last value were Real, apparently my program not recognize two different types in the same column.

To solve I change the type of field in the database from NUMERIC to REAL.