Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have 3 tables. 1 st table has all records, 2nd one has comments related to records and last one has replies related to all comments.
What i want is to display all records with comments and replies.
There can be 0 to n comments for each records. Same for replies.

My stored procedure is like:
ALTER PROCEDURE [dbo].[proc_ShowOpenSII]

AS  
BEGIN  
	SELECT 
		SII.SII_Id,
		SII.Title, 
		SII.Added_When,
		SII.Added_By,
		SII.Business_Owner,
		SII.Implementing_Cost,
		St.[Status],
		(Comm.Comments+' '+Comm.Comment_Added_By+' '+Rep.Replies+' '+Rep.Reply_Added_By) AS Notes
	FROM 
		Service_Improvement_Initiative AS SII 
		INNER JOIN Statuses AS St ON SII.Status_Id=St.Status_Id
		LEFT JOIN SII_Comments AS Comm ON SII.SII_Id=Comm.SII_Id
		LEFT JOIN SII_Replies AS Rep ON Comm.SII_Comment_Id=Rep.SII_Comment_Id
	WHERE
		SII.Status_Id <> '9'
END


But i am getting records with comments twice..
What can be wrong with this query?
Posted
Updated 23-Feb-12 22:42pm
v2
Comments
RAJI @Codeproject 24-Feb-12 4:50am    
use distinct after select

It is because you join it with replies table. you should first get all the comments first and then the replies on that comment.
Try by removing the join query on the replies table if the duplication would be gone.
 
Share this answer
 
Comments
tejashri.gandhi 24-Feb-12 5:04am    
Ok..I removed left join with replies table..but how will i get replies then?
graciax8 29-Feb-12 21:13pm    
You will get that using the commentID.

SELECT * FROM SII_Replies WHERE Rep.SII_Comment_Id = ''
you are using SII_Comments , and SII_Replies both tables in single Query that's by you are getting twice comments .......
 
Share this answer
 
Had you tried to use Distinct after select statement?
 
Share this answer
 

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



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