Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Group By does not return right result with multiple tabl
Message
From
23/01/2007 16:50:36
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 8 SP1
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01186807
Message ID:
01188364
Views:
23
>>>>>I changed your sample using GROUP BY, works fine. But I added one more insert
>>>>>to second cursor (INSERT INTO crsTest2 VALUES (1,100)) and now I get wrong value for Total Amount2 (first record)
>>>>>
>>>>>
>>>>>CREATE CURSOR crsTest  (Fld1 I)
>>>>>CREATE CURSOR crsTest1 (Fld1 I, amount n)
>>>>>CREATE CURSOR crsTest2 (Fld1 I, amount n)
>>>>>
>>>>>INSERT INTO crsTest VALUES (1)
>>>>>INSERT INTO crsTest VALUES (2)
>>>>>INSERT INTO crsTest VALUES (3)
>>>>>
>>>>>INSERT INTO crsTest1 VALUES (1,10)
>>>>>INSERT INTO crsTest1 VALUES (1,10)
>>>>>INSERT INTO crsTest1 VALUES (3,30)
>>>>>INSERT INTO crsTest1 VALUES (3,30)
>>>>>
>>>>>INSERT INTO crsTest2 VALUES (2,20)
>>>>>INSERT INTO crsTest2 VALUES (2,20)
>>>>>INSERT INTO crsTest2 VALUES (1,100)
>>>>>
>>>>>SELECT crsTest.Fld1, SUM(crsTest1.Amount) as Amount1, ;
>>>>>SUM(crsTest2.Amount) as Amount2;
>>>>>FROM crsTest;
>>>>>INNER JOIN crsTest1 ON crsTest.Fld1 = crsTest1.Fld1;
>>>>>INNER JOIN crsTest2 ON crsTest.Fld1 = crsTest2.Fld1;
>>>>>GROUP BY 1
>>>>>
>>>>>** (no records in the query)
>>>>>SELECT crsTest.Fld1, SUM(NVL(crsTest1.Amount,0)) as Amount1, SUM(NVL(crsTest2.Amount,0)) as Amount2;
>>>>>FROM crsTest;
>>>>>LEFT JOIN crsTest1 ON crsTest.Fld1 = crsTest1.Fld1;
>>>>>LEFT JOIN crsTest2 ON crsTest.Fld1 = crsTest2.Fld1;
>>>>>GROUP BY 1
>>>>>
>>>>>
>>>>
>>>>THIS WORKS FOR ME, the last select return 3 records.
>>>
>>>Fabio,
>>>
>>>I have also three records, but with wrong totals.
>>
>>
>>CREATE CURSOR crsTest  (Fld1 I)
>>CREATE CURSOR crsTest1 (Fld1 I, amount n)
>>CREATE CURSOR crsTest2 (Fld1 I, amount n)
>>
>>INSERT INTO crsTest VALUES (1)
>>INSERT INTO crsTest VALUES (2)
>>INSERT INTO crsTest VALUES (3)
>>
>>INSERT INTO crsTest1 VALUES (1,10)
>>INSERT INTO crsTest1 VALUES (1,10)
>>INSERT INTO crsTest1 VALUES (3,30)
>>INSERT INTO crsTest1 VALUES (3,30)
>>
>>INSERT INTO crsTest2 VALUES (2,20)
>>INSERT INTO crsTest2 VALUES (2,20)
>>INSERT INTO crsTest2 VALUES (1,100)
>>
>>** SUM aggregate this table, the left join multiply the set's rows
>>SELECT crsTest.Fld1, NVL(crsTest1.Amount,0) as Amount1, NVL(crsTest2.Amount,0) as Amount2;
>>FROM crsTest;
>>LEFT JOIN crsTest1 ON crsTest.Fld1 = crsTest1.Fld1;
>>LEFT JOIN crsTest2 ON crsTest.Fld1 = crsTest2.Fld1
>>
>>* you can use a union with pivot or a nested left join aggregation
>>
>>* remember, a SQL aggregate function ignore NULLs
>>
>>SELECT S1.Fld1,MAX(S1.Amount1) Amount1, NVL(SUM(crsTest2.Amount),00000000000) as Amount2;
>>FROM (SELECT crsTest.Fld1, NVL(SUM(crsTest1.Amount),00000000000) as Amount1;
>>	FROM crsTest;
>>	LEFT JOIN crsTest1 ON crsTest.Fld1 = crsTest1.Fld1 GROUP BY 1) S1;
>>LEFT JOIN crsTest2 ON S1.Fld1 = crsTest2.Fld1;
>> GROUP BY 1
>>
>Thanks Fabio, I think your last select statement does not run in VF8.

Right, vfp9 only
Previous
Reply
Map
View

Click here to load this message in the networking platform