Search code examples
sqlinterbase

Firebird SQL group by


I want to group by the column factd_poste_fact but I get this error:

SQL error code = -104.
Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause).

Code:

select
   f.fact_n,
   poste_facture ,
   sum_montant_ttc
from 
   factures f
left outer join
   (select
       facture_detail.fact_n,
       (facture_detail.factd_poste_fact) as poste_facture,
       sum(facture_detail.factd_montant_ttc) as sum_montant_ttc
    from 
       facture_detail
    group by    
       (fact_d.fact_n,facture_detail.factd_poste_fact)
group by
    f.fact_n, poste_facture, sum_montant_ttc

Solution

  • You needed to include facture_detail.fact_n in the group by in the sub query

    SELECT f.fact_n, poste_facture, sum_montant_ttc
    FROM factures f
    LEFT OUTER JOIN (SELECT fact_n, (factd_poste_fact) AS poste_facture, SUM(factd_montant_ttc) AS sum_montant_ttc
                     FROM facture_detail
                     GROUP BY factd_poste_fact, fact_n
                    ) fact_d ON (f.fact_n = fact_d.fact_n)
    
    GROUP BY f.fact_n, poste_facture, sum_montant_ttc