Someone has written the following stored proc query (I have removed all the irrelevant code to make things easier to read)
SELECT
CRL.ClientNum ,
..."A lot of columns"...,
ISNULL((SELECT SUM(Amount) FROM db..History dh (nolock) WHERE dh.id = dd.Id AND ActivityId = 11), 0.00) AS [Some Amt],
..."A lot more columns"...
FROM db..[SomeLog] SL
..."A lot of inner joins and left joins"...
where clause
order by clause
This all okay but, now I need to put in a Case statement to select/compare columns from The “db..History” table. The issue is there is no existing join on this table.
- I have tried putting in an inner join on the “db..History” table but this returns a lot more rows than it should. Most likely because there are multiple “Histories” for the table its joining on.
- I thought about using a temp table at the start to store the “db..History” table values but this required doing a lot of inner joins to get the data and would be performance hit.
Maybe your inner join isn’t correct, however, you can create a view of your necessary tables & fields and implement a join on that. in most situation using visual tools(shown in the above link) to create a view is easier and better than writing code.
Most join problems are because of creating tables incorrectly. If you create tables distributed truly and clear, it can help you a lot.