Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:22:06+00:00 2026-06-17T13:22:06+00:00

I have 2 tables built in this way: Trips – id – organization_id REQUIRED

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:22:08+00:00Added an answer on June 17, 2026 at 1:22 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a system built on seam/richfaces. There's this webpage where the tables are
I have an HTML table containing a few rows (this is built dynamically). All
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have two tables that were built for two disparate systems. I have records
So, I have built on this system for quite some time, and it is
The application I have uses localization. The way it is built, is that it
The corporate intranet I built heavily uses AJAX calls to load tables and other
This is my problem, I have a tiny PHP MVC framework i built. Now
the deal is this: I have a MySQL database that is built in this
I have built a web form with some tables and custom controls inside it,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.