So this query will fail if the existing data has a count column in it, so the simplest/best thing to do here is just use a field name like _sageworks_count and that way it won't conflict with existing column names.
# Combined query to get both top and bottom counts
query = (
f'(SELECT "{column}", count(*) as count '
f"FROM {table} "
f'GROUP BY "{column}" ORDER BY count DESC LIMIT 20) '
f"UNION ALL "
f'(SELECT "{column}", count(*) as count '
f"FROM {table} "
f'GROUP BY "{column}" ORDER BY count ASC LIMIT 20)'
)
So this query will fail if the existing data has a count column in it, so the simplest/best thing to do here is just use a field name like
_sageworks_count
and that way it won't conflict with existing column names.