Francesco Belladonna
Francesco Belladonna

Reputation: 11689

Write a big sql query or handle it through code?

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

Upvotes: 0

Views: 111

Answers (1)

Mr. Radical
Mr. Radical

Reputation: 1855

I'm not familiar with ruby on rails, but this is how you can do this with MySQL.

Sample data:

CREATE TABLE Trips(
  id int not null primary key,
  organization_id int not null,
  collaboration_organization_id int null
  );

INSERT INTO Trips (id,organization_id,collaboration_organization_id)
VALUES
(1,1,5),
(2,1,1),
(3,1,2),
(4,11,1),
(5,1,null),
(6,2,null),
(7,10,null),
(8,6,2),
(9,1,3),
(10,1,4);

MySQL Query:

SELECT organization_id,
sum(CASE WHEN collaboration_organization_id IS null THEN 1 ELSE 0.5 End) AS number
FROM Trips
GROUP BY organization_id;

Try it out via: http://www.sqlfiddle.com/#!2/1b01d/107

EDIT: adding collaboration organization

Sample data:

  CREATE TABLE Trips(
  id int not null primary key,
  organization_id int not null,
  collaboration_organization_id int null
  );

INSERT INTO Trips (id,organization_id,collaboration_organization_id)
VALUES
(1,1,5),
(2,1,1),
(3,1,2),
(4,11,1),
(5,1,null),
(6,2,null),
(7,10,null),
(8,6,2),
(9,1,3),
(10,1,4);


CREATE TABLE Organizations(
  id int auto_increment primary key,
  name varchar(30)
  );

INSERT INTO Organizations (name)
VALUES
("Org1"),
("Org2"),
("Org3"),
("Org4"),
("Org5"),
("Org6"),
("Org7"),
("Org8"),
("Org9"),
("Org10"),
("Org11"),
("Org12"),
("Org13"),
("Org14"),
("Org15"),
("Org16");

MySQL query:

SELECT O.id, O.name,
sum(CASE WHEN T.collaboration_organization_id IS null THEN 1 ELSE 0.5 End) AS number
FROM Organizations AS O LEFT JOIN Trips AS T  
ON T.organization_id = O.id OR T.collaboration_organization_id = O.id
WHERE T.collaboration_organization_id = O.id OR O.id = T.organization_id
GROUP BY O.id;

http://www.sqlfiddle.com/#!2/ee557/15

Upvotes: 2

Related Questions