Subject Re: [ib-support] joins in sql
Author Lucas Franzen
"harri007et " schrieb:
>
> Hi!
>
> Given the following query, how are the count(A.STATUS) and
> sum(A.STATUS) really calculated?
>
> SELECT COUNT(A.STATUS), SUM(A.STATUS)
> FROM TABLE1 A
> JOIN TABLE2 B ON B.L_ID = A.O_ID
> LEFT OUTER JOIN TABLE3 C ON C.O_ID = B.L_ID
> LEFT OUTER JOIN TABLE1 D ON D.O_ID = B.L_ID
> LEFT OUTER JOIN TABLE3 E ON E.ID = A.OS_ID
> LEFT OUTER JOIN TABLE1 F ON F.OS_ID = E.ID
>
> (Table1 has 1152 rows, column STATUS is type integer and has
> 1049 "0"-s and 76 "1"-s, query result is 16036, 1185)
>
> What should I do to get "correct" results?

What is the correct result?


1. Each record from table A that has more than one corresponding entry
in any of the joined tables will be counted several times, too. So I'm
not sure if this is really what you want.
(What are the joins for anyway???)

2. Are you sure, you won't group your result, so that you can see the
number of records per status?
But remember the total has to be built by you either by another query or
by walking through your resultset, summing the count(*) of the grouped
records

Luc.