How might I split data into many result sets?

I have some data I load from a database. The data contains a field that can be set to one of X number of different values. One query might return 1 unique field value, another query may return 5 unique field values, or any number of unique values.

I want to split that data into different ‘bins’ depending on what value that field is set to.

Once I load the data, I know how many different values that field contains and what the unique values are.

Is there an ‘elixir’ way to build a generic function that can do the splitting out of the data records, with an indeterminate number of different bins that will be needed to hold the split data - adding bins as it needs to? Something like a 2 dimensional array like this:

split_data[0] = [the_data_fields_for_a_data_row] # bin one
split_data[1] = [the_data_fields_for_a_data_row] # bin two
split_data[n] = [the_data_fields_for_a_data_row] # bin 'n'

No, I can not run separate queries to split the data because that defeats my purpose (I want to hit the database only once and let the code do the splitting so I keep my app running quickly).

Sounds like a job for Enum.group_by/2. Something like Enum.group_by(all_the_rows, &(&1.selector_field)) where selector_field is the field you are using to decide which bins the rows go into.

Thanks - I’ll give that a try.