|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
SQL Statement Help
I have 3 different tables with the same structure. Each row has an ID. Some of the IDs are the same in different tables. I need to add together the data from certain columns of different tables where the IDs are the same, but also include every ID that doesn't match in one of the other tables. For example:
Table 1: ID, hits, walks 12 2 3 13 4 2 14 4 3 Table 2: ID, hits, walks 12 3 5 15 2 2 16 1 3 Table 3: ID, hits, walks 12 2 1 13 4 2 16 2 3 The SQL statement should return this: ID, hits, walks 12 7 9 13 8 4 14 4 3 15 2 2 16 3 6 I'm going to put this data into an array on my webpage and use the array with ASP and VBscript to calculate other things and form my page. Maybe there is a better way to create this array using code instead of SQL. Can anyone help me out? Thanks. Steve |
|
#2
|
||||
|
||||
|
Join your data together using an INNER JOIN. To get the data that doesn't match, use an OUTER JOIN. Once you join your data, you can add it using the SUM function in SQL. Hope this helps.
|
|
#3
|
|||
|
|||
|
Didn't test the below query but I think you'll get the idea...
Sum(MySubQuery.hits) as hits, Sum(MySubQuery.walks) as walks FROM (SELECT Sum(hits) as hits, Sum(walks) as walks FROM Table 1 GROUP BY ID UNION SELECT Sum(hits) as hits, Sum(walks) as walks FROM Table 2 GROUP BY ID UNION SELECT Sum(hits) as hits, Sum(walks) as walks FROM Table 3 GROUP BY ID) AS MySubQuery |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > SQL Statement Help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|