Subject Re: [firebird-support] SQL sintax error
Author Helen Borrie
At 08:34 6/10/2008, you wrote:
>using this SQL command
>
>select pat.cfg_tipo, pat.cfg_codice, sum(pat.importo_dovuto) importo_dovuto,
>
>coalesce((select sum(importo_pagato) from pas where pas.progressivo =
>pat.progressivo), 0) importo_pagato
>from pat
>where pat.cfg_tipo = 'C'
>
>I receive this error
>
>Invalid expression in the select list (not contained in either an aggregate
>function or the GROUP BY clause).
>
>
>what I have to do? ...

1. To have cfg_codice involved in the aggregation you have to include it in the Group By clause. (It is not part of the implicit "whole table" group.)

2. Both of the aggregations need to be in the same grouping.

3. In multi-table queries, all referenced columns should be qualified.

4. It's not always a good convention to use an existing column name to alias an expression field.

Try the following to see whether it gets the set you are looking for:

select
pat.cfg_tipo,
pat.cfg_codice,
sum(pat.importo_dovuto) s_importo_dovuto,
coalesce(
sum(pas.importo_pagato), 0) s_importo_pagato
from pat
left join pas
on pas.progressivo = pat.progressivo
where pat.cfg_tipo = 'C'
group by 1,2

./hb