I made some assumptions about the actual structure of your tables, but if I understand what you are looking for, I think this query will get the desired results. You may need to make a few changes to suit your table structures.
SELECT matches.UserName, CAST(matches.SameRatings AS FLOAT) / CAST(ratings.UserRatingCount AS FLOAT) AS MatchPercent FROM tbl_User CROSS APPLY ( SELECT COUNT(*) UserRatingCount FROM tbl_MemberAssociation WHERE UserId = tbl_User.UserId ) ratings CROSS APPLY ( SELECT u1.UserId, u1.UserName, COUNT(*) AS SameRatings FROM tbl_MemberAssociation ma INNER JOIN tbl_MemberAssociation ma1 ON ma.ImageId = ma1.ImageId AND ma.Rating = ma1.Rating AND ma.UserId <> ma1.UserId INNER JOIN tbl_User u1 ON ma1.userId = u1.UserId WHERE ma.UserId = tbl_User.UserId GROUP BY u1.UserId, u1.UserName ) matches WHERE tbl_User.UserId = @UserId ORDER BY MatchPercent DESC
@UserId can be passed as input to a stored procedure.
The 1st CROSS APPLY rating receives an invoice for the total number of ratings for a registered user.
The second CROSS APPLY "match" gets the number of similar ratings for other users in the database.
The result set uses calculations calculated by two CROSS APPLY queries to calculate the percent match between the registered user and other users who rated the same images as the registered user.
source share