jeudi 6 juin 2019

ActiveRecord query to retrieve frequency depending on a condition

Please, read everything

Hello, I need to make some more or less complex calculations (at least for me), I've made a query with several joins to combine several tables and select some columns, but now I have to calculate the frequency of a field, this field is deliver.status_id, the problem is I have to do this calculation taking into account only one deliver_group.id, what do I mean by this? let's make an example:

If I have these selected columns...

|---------------------|------------------|
|  deliver.status_id  | deliver_group.id |
|---------------------|------------------|
|          1          |         10       |
|---------------------|------------------|
|          3          |         11       |
|---------------------|------------------|
|          4          |         12       |
|---------------------|------------------|
|          2          |         12       |
|---------------------|------------------|
|          1          |         12       |
|---------------------|------------------|
|          2          |         13       |
|---------------------|------------------|
|          3          |         13       |
|---------------------|------------------|

Then the deliver.status_id frequency would be {1 => 2, 2 => 2, 3 => 2 4 => 1}, but as I said I only have to take into account the one occurrence of deliver_group.id, this means the frequency would be {1 => 1, 2 => 1, 3 => 1, 4 => 1}, cause I have to "ignore" deliver.status_id when the deliver_group.id is repeated.

|---------------------|------------------|
|  deliver.status_id  | deliver_group.id |
|---------------------|------------------|
|          1          |         10       |
|---------------------|------------------|
|          3          |         11       |
|---------------------|------------------|
|          4          |         12       |
|---------------------|------------------|
|          2          |   12 (ignored)   |
|---------------------|------------------|
|          1          |   12 (ignored)   |
|---------------------|------------------|
|          2          |         13       |
|---------------------|------------------|
|          3          |   13 (ignored)   |
|---------------------|------------------|

Another example:

|---------------------|------------------|
|  deliver.status_id  | deliver_group.id |
|---------------------|------------------|
|          1          |         7        |
|---------------------|------------------|
|          3          |         8        |
|---------------------|------------------|
|          4          |         9        |
|---------------------|------------------|
|          2          |    9 (ignored)   |
|---------------------|------------------|
|          1          |         10       |
|---------------------|------------------|
|          2          |         11       |
|---------------------|------------------|
|          3          |   11 (ignored)   |
|---------------------|------------------|
|          3          |   11 (ignored)   |
|---------------------|------------------|
|          3          |         12       |
|---------------------|------------------|
|          3          |   12 (ignored)   |
|---------------------|------------------|
|          3          |   12 (ignored)   |
|---------------------|------------------|

Would result in {1 => 2, 2 => 1, 3 => 2, 4 => 1}.

By the way, I don't want to do this with ruby syntax, I mean, I know how to do this with a Hash and an iterator, blah, blah, blah, but I want to make the calculations completely with a query because the performance is key in this app

I know the case where nothing is ignored is quite simple, query.group('deliver.status_id').count, but I'm lost with my actual case.

Aucun commentaire:

Enregistrer un commentaire