Q: Joining SQL tables when 2nd table comes from SQL query I am very new to programming and I have trouble getting the SQL syntax right to join the following two tables (the dots indicate other columns which are not relevant). 
T1: Code1, name, ...
T2: Code2, value, ID1, ID2, ...
SELECT code1, name FROM T1 
--> produces table 1
SELECT Code2, SUM(value) FROM T2 
WHERE ID1 = X and ID2 = X
GROUP BY Code 2
--> produces table 2
I now want to join tables 1 and 2 ON TRIM(T1.code1) = TRIM(T2.code2) (trim removes spaces causing mismatch).
T1 contains entries that have no value, which I want to appear. So I tried having T1 first and using LEFT OUTER JOIN, but I can't seem to get it right. Any suggestions?
Thanking you in advance,
George
EDIT1 - As I haven't stated the platform, the SQL string will be used in MS Access to fill a listbox with the rowsource command.
EDIT2 - SAMPLE DATA AND DESIRED RESULTS
T1:
Code1   | Name
---------------
1       |  a
2       |  b
3       |  c
4       |  d

T2
Code 2 | Value
---------------
1      |   10
1      |   20
1      |   25
2      |   5
2      |   10
4      |   30
4      |   60

Desired results:
Code  | Name  |  Total
1     |   a   |   55
2     |   b   |   15
3     |   c   |   0 (or NULL)
4     |   d   |   90

Hope this helps

A: Simply surround your queries with parentheses:
select trim(q1.code1) as code, q1.name, q2.total
from (SELECT code1, name FROM T1 ) q1
left join
(
  SELECT Code2, SUM(value) as total FROM T2 
  WHERE ID1 = X and ID2 = X
  GROUP BY Code2
) q2 on trim(q2.code2) = trim(q1.code1);

In your case where you don't have any WHERE clause on table1, you can reduce this to:
select trim(t1.code1) as code, t1.name, q2.total
from t1
left join
(
  SELECT Code2, SUM(value) as total FROM T2 
  WHERE ID1 = X and ID2 = X
  GROUP BY Code2
) q2 on trim(q2.code2) = trim(t1.code1);


A: I guess your query could be:
SELECT T1.code1, T1.name, T3.tot
FROM T1 LEFT OUTER JOIN
(SELECT Code2, SUM([value]) AS tot
    FROM T2 WHERE ID1 = X and ID2 = X
    GROUP BY Code2) T3
ON TRIM(T1.code1) = TRIM(T3.code2)

