Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » DBISAM Technical Support » Support Forums » DBISAM SQL » View Thread |
Messages 1 to 6 of 6 total |
Sum() in two tables from each ID in another table |
Wed, May 24 2006 8:29 AM | Permanent Link |
Davis | I have three tables:
TABLE_1 client_ID Client_Name TABLE2 Client_ID Value1 TABLE3 client_ID Value2 I am trying to get a query to show Client_Name from table_1, the sum of value1 from TABLE2 for each client_ID in TABLE1, and the sum of value2 from TABLE3 for each client_ID TABLE1. How can i do it ? Thanks, Davis |
Wed, May 24 2006 5:14 PM | Permanent Link |
Jeff Cook | Davis <davis@davis.com> wrote on Wed, 24 May 2006 08:29:19 -0400
>I have three tables: > >TABLE_1 >client_ID >Client_Name > >TABLE2 >Client_ID >Value1 > >TABLE3 >client_ID >Value2 > >I am trying to get a query to show Client_Name from table_1, >the sum of value1 from TABLE2 for each client_ID in TABLE1, and the sum >of value2 from TABLE3 for each client_ID TABLE1. >How can i do it ? > >Thanks, >Davis > > Davis SELECT T1.Client_ID,T1.Client_Name, SUM(T2.Value1), SUM(T3.Value2) FROM TABLE1 T1 LEFT OUTER JOIN TABLE2 T2 ON (T2.Client_ID = T1.Client_ID) LEFT OUTER JOIN TABLE3 T3 ON (T3.Client_ID = T1.Client_ID) GROUP BY T1.Client_ID, T1.Client_Name Cheers Jeff -- Jeff Cook Aspect Systems Ltd Phone: +64-9-424 5388 Skype: jeffcooknz www.aspect.co.nz |
Wed, May 24 2006 10:35 PM | Permanent Link |
Davis | Thanks, but i get a wrong result.
Davis. Jeff Cook <jeffc@aspect.co.nz> wrote: Davis <davis@davis.com> wrote on Wed, 24 May 2006 08:29:19 -0400 >I have three tables: > >TABLE_1 >client_ID >Client_Name > >TABLE2 >Client_ID >Value1 > >TABLE3 >client_ID >Value2 > >I am trying to get a query to show Client_Name from table_1, >the sum of value1 from TABLE2 for each client_ID in TABLE1, and the sum >of value2 from TABLE3 for each client_ID TABLE1. >How can i do it ? > >Thanks, >Davis > > Davis SELECT T1.Client_ID,T1.Client_Name, SUM(T2.Value1), SUM(T3.Value2) FROM TABLE1 T1 LEFT OUTER JOIN TABLE2 T2 ON (T2.Client_ID = T1.Client_ID) LEFT OUTER JOIN TABLE3 T3 ON (T3.Client_ID = T1.Client_ID) GROUP BY T1.Client_ID, T1.Client_Name Cheers Jeff -- Jeff Cook Aspect Systems Ltd Phone: +64-9-424 5388 Skype: jeffcooknz www.aspect.co.nz |
Thu, May 25 2006 12:51 AM | Permanent Link |
Jeff Cook | Davis <davis@davis.com> wrote on Wed, 24 May 2006 22:35:05 -0400
>Thanks, but i get a wrong result. > > > >Davis. > Davis Wrong as in "error message" or wrong as in ... what? Cheers Jeff -- Jeff Cook Aspect Systems Ltd Phone: +64-9-424 5388 Skype: jeffcooknz www.aspect.co.nz |
Thu, May 25 2006 7:27 AM | Permanent Link |
Davis | Jeff Cook <jeffc@aspect.co.nz> wrote:
Davis <davis@davis.com> wrote on Wed, 24 May 2006 22:35:05 -0400 >Thanks, but i get a wrong result. > > > >Davis. > Davis Wrong as in "error message" or wrong as in ... what? Cheers Jeff -- Jeff Cook Aspect Systems Ltd Phone: +64-9-424 5388 Skype: jeffcooknz www.aspect.co.nz The result of sql is not correct. The sum of table 3 ( t3.value2) show me a incorrect sum. See: T1 ID-------NAME 1 Jonh 2 Robert t2 ID------Value 1 10.00 1 10.00 2 20.00 2 20.00 t3 ID-------Value 1 10.00 1 10.00 2 10.00 2 10.00 Expected Result ID----------------Name-----------------Value T2----------------Value T3 1 Jonh 20.00 20.00 2 Robert 40.00 20.00 Wrong Result ID----------------Name-----------------Value T2----------------Value T3 1 Jonh 40.00 40.00 2 Robert 80.00 40.00 |
Thu, May 25 2006 8:45 AM | Permanent Link |
"John Hay" | Davis
> I am trying to get a query to show Client_Name from table_1, > the sum of value1 from TABLE2 for each client_ID in TABLE1, and the sum > of value2 from TABLE3 for each client_ID TABLE1. > How can i do it ? You can do this in DBISAM with a script like SELECT Client_Id, SUM(Value1) AS Value1 INTO Memory\Temp1 FROM Table_2 GROUP BY Client_Id; SELECT Client_Id, SUM(Value1) AS Value2 INTO Memory\Temp2 FROM Table_3; GROUP BY Client_Id; SELECT Client_Id,Client_Name,Value1,Value2 FROM Table_1 LEFT OUTER JOIN Memory\Temp1 T1 ON T1.Client_Id=Table_1.Client_Id LEFT OUTER JOIN Memory\Temp2 T2 ON T2.Client_Id=Table_1.Client_Id Remember to drop the memory tables when you are done. John |
This web page was last updated on Sunday, May 5, 2024 at 10:18 AM | Privacy PolicySite Map © 2024 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |