I'm getting a cartesian product that I don't want in a complex Informix SQL query.
I know which of the tables is causing the problem, but I can't figure out a way around the cartesian product.
The query I refer to is very long, but I am going to exemplify the problem with only two tables:
SELECT ROUND( SUM( CASE WHEN ccp.IdConcepto IN ( 60, 62 ) THEN ccp.Importe END ), 2 ) TotalInvoiced
FROM Cbts cc, Cbts_Cpts ccp
WHERE cc.CbteId = ccp.CbteId
AND ccp.IdConcepto IN ( 10, 30, 31, 32 )
The table called Cbtes has a primary key called CbteId, which is not the primary key of Cbtes_Cptos. Cbtes_Cptos also has other columns, including IdConcepto (which I use in the query), IdSubConcepto, Unidades, Importe, etc.).
The problem is that in Cbtes_Cptos for the value 10 there are from 1 to 4 different records, and this difference is marked by the "IdSubConcepto" column, which can range from 1 to 4. The relationship can be the following:
Cbtes Cbtes_Cptos
IdCbte IdCbte IdConcepto IdSubConcepto
1 1 10 1
1 10 2
1 10 3
1 10 4
1 60 0
1 62 0
So, the query returns the sum of several times the value of "Importe".
And I need to use the IdConcepto in the WHERE to speed up the query since it can have more than 5000 different values.
I hope I have explained the problem well, and I appreciate your attention.