Subject Re: [ib-support] Select Count(*) strange Result
Author Arno Brinkman
Hi,

> at the first glance, Firebird 1.0 has as strange result from a
> count(*) Statement:
>
> The following sql fetches exactly 1 row as expected:
>
> select
> WBG.VERKAUFSKONTO
> , A.UMSATZSTEUERCODE
> , Sum(BBZ.BRUTTOBETRAG) as BRUTTOBET
> , Sum(BBZ.NETTOBETRAG) as NETTOBET
> , Sum(BBZ.MWSTBETRAG) as MWSTBET
> FROM BBELEGZEILEN BBZ
> JOIN ARTIKEL A on (BBZ.ARTIKELNR=A.ARTIKELNR)
> JOIN WARENBUCHUNGSGRUPPEN WBG on
(A.BUCHUNGSGRUPPE=WBG.WARENBUCHUNGSGRUPPE)
> where BELEGID=8
> GROUP BY WBG.VERKAUFSKONTO, A.UMSATZSTEUERCODE
> HAVING Sum(BBZ.NETTOBETRAG) > 0
>
> But the corresponding counting select returns 16:
>
> select count(*)
> FROM BBELEGZEILEN BBZ
> JOIN ARTIKEL A on (BBZ.ARTIKELNR=A.ARTIKELNR)
> JOIN WARENBUCHUNGSGRUPPEN WBG on
(A.BUCHUNGSGRUPPE=WBG.WARENBUCHUNGSGRUPPE)
> where BELEGID=8
> GROUP BY WBG.VERKAUFSKONTO, A.UMSATZSTEUERCODE
> HAVING Sum(BBZ.NETTOBETRAG) > 0
>
> Any explanation/help for me?

Count(*) returns the number of rows that are "grouped". In your case the
number of distinct rows from WBG.VERKAUFSKONTO and A.UMSATZSTEUERCODE. Add
to Count(*) to your first query and you'll see.

So there's nothing wrong :-)

Regards,
Arno