I have 2 tables,

Table1:

Code:

---------------------
| col1 | col2 |
---------------------
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
---------------------

Table2:

Code:

---------------------
| col1 | col2 |
---------------------
| 5 | C |
| 2 | C |
| 4 | B |
| 7 | B |
| 8 | B |
---------------------

This is the sql statement i am using...

Code:

Select table1.col1, table1.col2,
(Select SUM(table2.col1) FROM table2 where table2.col2 = table1.col2) as MySum
FROM
Table1
Where
(Select SUM(table2.col1) FROM table2 where table2.col2 = table1.col2)) > 8

Basically what i am trying to do is, get the sum from table2.col1 where table2.col2 and table1.col2 have the same value... and also want to fiilter the results using the sum

expected result:

Code:

--------------------------------
| col1 | col2 | mysum |
--------------------------------
| 2 | B | 19 |
| 2 | C | 7 | * this row will not show since the sum is else than 8 (from the where clause)
--------------------------------

* Using MS SQL