If I'm interpreting you SQL correctly:
- you have two separate sql statements
- first one returns 3 rows
- second one returns 2 rows
- both queries are independent (based on SQL syntax)
- the results from both queries are combined by union.
So this means that there is no condition in the first query that would exclude the blog id from the second query or vice versa. This means that the solution is data specific.
Now without knowing the relations between those tables, I'm just guessing but perhaps you could modify the query to something like:
select Top 3 *
from (select blogid,
COUNT(*) counts
from Blog_Comment
where isAdmin=0
and isActive=1
group by BlogId
order by counts desc ) b
union
select top 2 *
from (select BlogId,
SUM(Likecount) counts
from Blog_Like
where PartnerId not like 'MS%'
and BlogId not in (select top 3
blogid,
COUNT(*) counts
from Blog_Comment
where isAdmin=0
and isActive=1
group by BlogId
order by counts desc)
group by BlogId
order by counts desc ) a order by counts
Most likely the query could by simplified a lot but that would require some knowledge about the data model.