Gijs de Wert
Gijs de Wert

Reputation: 37

Percentage from SUM in SQL

I am building an application to see the progress of students.

I made a table active_projects. In here I got : active_project_hours (the amount of hours they get from finishing) active_project_percent (how far are they? as int, for example 50. This means 50%)

The problem is, I am fetching the data now like this :

"SELECT SUM(finished_project_hours) AS totalDone 
 FROM finished_projects 
 WHERE finished_project_user_id = {$_SESSION['user']['user_id']}";

But I want that if there for example is filled in :

active_project_hours = 10
active_project_progress = 20

that it does not sum the 10 hours, but 10 hours - 20 percent, so 8 hours

Can someone please help me?

Thanks!

Upvotes: 0

Views: 47

Answers (1)

Barmar
Barmar

Reputation: 781059

Use:

SELECT SUM(active_project_hours * (1 - (active_project_progress/100)) AS timeRemaining

Upvotes: 1

Related Questions