I have 2 tables built in this way:
Trips
- id
- organization_id REQUIRED
- collaboration_organization_id OPTIONAL
...other useless fields...
Organizations
- id
- name REQUIRED
...other useless fields...
Now I have been asked to create this type of report:
I want the sum of all trips for each organization, considering that if they have a collaboration_organization_id it should count as 0.5, obviusly the organization in collaboration_organization_id get a +0.5 too
So whenever I have a trip that has organization_id AND collaboration_organization_id set, that trip count as 0.5 for both organizations. If instead only organization_id is set, it counts as 1.
Now my question is composed by two parts:
1.
Is a good idea to "solve" the problem all in SQL?
I already know how to solve it through code, my idea is currently "select all trips (only those 3 fields) and start counting in ruby". Please consider that I'm using ruby on rails so could still be a good reason to say "no because it will work only on mysql".
2.
If point 1 is YES, I have no idea how to count for 0.5 each trip where it's required, because count is a "throw-in-and-do-it" function
sum(if(...your credit logic here...))as well.