Use a relation to build up the query from the common parts
Remove duplicate tags_count code