Suppose I have two tables:
Table A has information about the stock holdings of different investment funds:
FundID FundType StockID Weight
1 Small A 0.50
1 Small B 0.50
2 Small B 0.25
2 Small C 0.75
3 Large A 1.00
4 Large D 1.00
Table B summarizes the weight each stock has within each type of fund:
FundType StockID AvgWeight
Small A 0.25
Small B 0.375
Small C 0.375
Large A 0.5
Large D 0.5
(Note that if a stock isn't listed for a fund, then the weight is assumed to be 0.)
I want to create a new table (with SQL) that has, for each FundID in Table A, all stocks that are held by any fund of the same type:
Desired results table:
FundID FundType StockID Weight AvgWeight
1 Small A 0.50 0.25
1 Small B 0.50 0.375
1 Small C 0.375
2 Small A 0.25
2 Small B 0.25 0.375
2 Small C 0.75 0.375
3 Large A 1.00 0.5
3 Large D 0.5
4 Large A 0.5
4 Large D 1.00 0.5
Notice that all stocks that are held by any small fund (A, B, and C) are included for each small fund (FundID is 1 or 2). Similarly, all stocks that are held by any large fund (A and D) are included in the results for each large fund.
I think I need to do some kind of outer join, but have so far been unable to figure out the SQL syntax. Any ideas? Thanks!