Thread: MORE SQL Help
View Single Post
  #4 (permalink)  
Old 03-04-08, 02:46 PM
uwila's Avatar
uwila uwila is offline
Do nothing at ground level
 

Join Date: Apr 2006
Location: New Malden, Surrey
Posts: 1,767
uwila is never out of the wateruwila is never out of the wateruwila is never out of the wateruwila is never out of the wateruwila is never out of the wateruwila is never out of the wateruwila is never out of the wateruwila is never out of the wateruwila is never out of the wateruwila is never out of the wateruwila is never out of the water
try the following:

SELECT V.ValuationID AS VID, VR.DBLiabFunding AS JK7, VR.MPLiabFRS17OrIAS19 AS JK32, VD.PlanLenYears AS M12, F.InputStage AS ISTAGE
FROM Valuations V
INNERJOIN

(
select COALESCE(F.ValuationID,P.ValuationID) ValuationID, COALESCE(F.InputStage,P.InputStage) InputStage
from
(select ValuationID, InputStage
from Financial F where (InputStage = 1)) P
left outer join
(select ValuationID, InputStage
from Financial F where (InputStage = 2)) F on F.ValuationID = P.ValuationID) F
ON F.ValuationID = V.ValuationID

INNERJOIN ValuationResults VR ON VR.ValuationID = V.ValuationID AND F.InputStage = VR.InputStage
INNERJOIN ValuationDocuments VD ON VD.ValuationID = V.ValuationID AND F.InputStage = VD.InputStage
WHERE (VR.DBLiabFunding ISNOTNULL)AND(VR.MPLiabFRS17OrIAS19 ISNOTNULL)AND(VD.PlanLenYears ISNOTNULL)

The bit in red creates a temporary table which contains rows for final records and prelim records if no final record exists. Note that if a final record exists without a prelim record, then this one will be excluded.

I haven't tested the full query, but the red bit does exactly what I said

Cheers, Chris
__________________
The man who's more anal than Kirstie - Turbanator
Reply With Quote