答案 0 :(得分:2)
您的要求非常不明确,但根据您的评论,您使用Table1
作为根据喜欢,评论和观看次数收到的徽章的查找表。
您可以为每个元素执行Cross Apply
这些查找:
Select UserId, Likes, Comments, Views,
LikeBadges.Count + CommentBadges.Count + ViewBadges.Count As NoOfBadges
From Table2
Cross Apply
(
Select Count(*) as Count
From Table1
Where Table1.Action = 'Likes'
And Table2.Likes >= Table1.Count
) As LikeBadges
Cross Apply
(
Select Count(*) as Count
From Table1
Where Table1.Action = 'Comments'
And Table2.Comments >= Table1.Count
) As CommentBadges
Cross Apply
(
Select Count(*) as Count
From Table1
Where Table1.Action = 'Views'
And Table2.Views >= Table1.Count
) As ViewBadges