Thursday, September 5, 2013

SQL Get records from table where related objects dont met criteria

SQL Get records from table where related objects dont met criteria

SELECT PRH.prid, PRH.CreationDate, PRH.ClosedDate, PRA.creationdate
FROM PRHeader PRH
left join PRAction PRA on PRH.PrId = PRA.PrId
WHERE
PRA.CreationDate =
(select min(CreationDate ) from PRAction pa WHERE PRH.prid=pa.prid
and pa.ActionTypeId<>4)
OK I'm not sure how to explain this... But I have one table PRHeader with
objects every object in PRHeader has 0-many related actions in PRAction.
The above code gets the earliest action for each object that has the
relevant actiontype , because I want the timestamp from that one. Problem
is that some objects dont have any action that fits this criteria and then
I still want the object returned but with null as the action time. And
obviously I only want each object once. Any advice?

No comments:

Post a Comment