Subject Re: Problem with LEFT OUTER JOIN and COUNT()
Author Svein Erling Tysvær
Try

SELECT COUNT(DISTINCT N.EMISSAO||N.CUPOM||N.OBS1||N.OBS3||V.DESCRICAO)
FROM SIAD_VND N
LEFT OUTER JOIN SIAD_VED V ON (V.CODIGO = N.VEDEXT)
WHERE TIPO='V' AND N.OBS3 LIKE '%SUS%'

You will probably have to tweak this a bit depending on the values in
STAD_VND - e.g. N.EMISSAO = 'Fabio' and N.CUPOM = '' would be
considered equal to N.EMISSAO = 'Fa' and N.CUPOM = 'bio' in the query
above and only counted once.

SELECT DISTINCT COUNT(*) doesn't count distinct values, it makes sure
that the number of rows is returned only once (something your query
does anyway)!

HTH,
Set

--- In firebird-support@yahoogroups.com, "Fabio Gomes" wrote:
>
> Hi guys,
>
> I have a pagination function in php that uses a counter to generate
> the page links and stuff, it always worked fine, but i m having a
> problem with this query:
>
> I m using this to count the results:
>
> SELECT DISTINCT COUNT(*)
> FROM SIAD_VND N
> LEFT OUTER JOIN SIAD_VED V ON (V.CODIGO = N.VEDEXT)
> WHERE TIPO='V' AND N.OBS3 LIKE '%SUS%'
>
> And this to show them:
>
> SELECT DISTINCT N.EMISSAO, N.CUPOM, N.OBS1, N.OBS3, V.DESCRICAO
> FROM SIAD_VND N
> LEFT OUTER JOIN SIAD_VED V ON (V.CODIGO = N.VEDEXT)
> WHERE TIPO='V' AND N.OBS3 LIKE '%SUS%'
>
>
> But the problem is that the COUNT() returns me 2000+ rows, and the
> query return just 1000, i tried to change the query and stuff. but
> it isnt working, i think that it have something to do with the LEFT
> OUTER JOIN.
>
> Am i doing something wrong?
>
> Anyway, any help is welcome.
>
> Thanx,
>
> -Fábio.