Queries and functions to prep data for bulkloading