Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.73/5 (4 votes)
See more:
Please find the fiddle here
My requirement is I need to display the latest record from 3rd table if exists based on first table ID and second table ID, if not I would like to display the latest record from second table. I have given the sample tables and the query I have written but i am not getting the actual result as per my need so some one can help me
SQL Fiddle

This is what I am getting and I have written what I expected

http://i.stack.imgur.com/KCFkA.png[^]
Posted
Updated 28-Jan-16 22:08pm
v3
Comments
dan!sh 29-Jan-16 3:51am    
+5 for being one of the few who post proper question.
jaket-cp 29-Jan-16 6:55am    
why do you consider this to be a proper question?
if you have to link off to another site to get the full details of the question

1 solution

I can't access the image URL from work. But I guess I may have understood what you need. You can add more columns to the query.

SQL
SELECT	ISNULL(CDESC,BDESC) 
FROM	TBLA JOIN TBLB 
ON		TBLA.AID = TBLB.AID
		LEFT JOIN TBLC 
ON		(TBLB.BID = TBLC.BID AND TBLB.AID = TBLC.AID)
ORDER BY TBLC.CID DESC,TBLB.BID DESC
 
Share this answer
 
Comments
Krishna Chaitanya 29-Jan-16 5:24am    
Hi danish I need only one row against each tblA.AID and it should be the latest, the query you have given me is giving multiple rows. Here is the fiddle I am attaching what I need is I need only 'Post2 Regarding A' from AID 10
<link>http://sqlfiddle.com/#!6/33b0e/3</link>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900