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

  • Home
  • SEARCH
  • 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 8034117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:54:37+00:00 2026-06-05T01:54:37+00:00

I have the following table: +—–+———–+———-+————+——+ | key | idStudent | idCourse | hourCourse

  • 0

I have the following table:

+-----+-----------+----------+------------+------+
| key | idStudent | idCourse | hourCourse | mark |
+-----+-----------+----------+------------+------+
|   0 |         1 |        1 |         10 |   78 |
|   1 |         1 |        2 |         20 |   60 |
|   2 |         1 |        4 |         10 |   45 |
|   3 |         3 |        1 |         10 |   90 |
|   4 |         3 |        2 |         20 |   70 |
+-----+-----------+----------+------------+------+

Using a simple query, I can show student with their weighted average according to hourCourse and mark:

SELECT idStudent,
       SUM( hourCourse * mark ) / SUM( hourCourse ) AS WeightedAvg
  FROM `test`.`test`
  GROUP BY idStudent;
+-----------+-------------+
| idStudent | WeightedAvg |
+-----------+-------------+
|         1 |     60.7500 |
|         3 |     76.6667 |
+-----------+-------------+

But now I need to select the registers until the cumulative sum of hourCourse per student reaches a threshold. For example, for a threshold of 30 hourCourse, only the following registers should be taken into account:

+-----+-----------+----------+------------+------+
| key | idStudent | idCourse | hourCourse | mark |
+-----+-----------+----------+------------+------+
|   0 |         1 |        1 |         10 |   78 |
|   1 |         1 |        2 |         20 |   60 |
|   3 |         3 |        1 |         10 |   90 |
|   4 |         3 |        2 |         20 |   70 |
+-----+-----------+----------+------------+------+

key 2 is not taken into account, because idStudent 1 already reached 30 hourCourse with idCourse 1 and 2.

Finally, the query solution should be the following:

+-----------+-------------+
| idStudent | WeightedAvg |
+-----------+-------------+
|         1 |     66.0000 |
|         3 |     76.6667 |
+-----------+-------------+

Is there any way to create an inline query for this? Thanks in advance.

Edit: The criteria while selecting the courses is from highest to the lowest mark.
Edit: Registers are included while the cumulative sum of hourCourse is less than 30. For instance, two registers of 20 hours each would be included (sum 40), and the following not.

  • 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-05T01:54:39+00:00Added an answer on June 5, 2026 at 1:54 am

    You can calculate the cumulative sums per idStudent in a sub-query, then only select the results where the cumulative sum is <= 30:

    select idStudent,
           SUM( hourCourse * mark ) / SUM( hourCourse ) AS WeightedAvg
    from
    (
      SELECT t.*,
      case when @idStudent<>t.idStudent
        then @cumSum:=hourCourse
        else @cumSum:=@cumSum+hourCourse
      end as cumSum,
      @idStudent:=t.idStudent
      FROM `test` t,
      (select @idStudent:=0,@cumSum:=0) r
      order by idStudent, `key`
    ) t
    where t.cumSum <= 30
    group by idStudent;
    

    Demo: http://www.sqlfiddle.com/#!2/f5d07/23

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

Sidebar

Related Questions

I have the following table structure Key int MemberID int VisitDate DateTime How can
I'm using: Rails 3.0.9 Activerecord-sqlserver-adapter 3.0.15 TinyTds MSSQL 2005 I have following table: CREATE
I am having issues with a sql query. I have following table (I simplified
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have following table structure. town: id (MEDINT,PRIMARY KEY,autoincrement), town(VARCHAR(150),not null), lat(FLOAT(10,6),notnull) lng(FLOAT(10,6),notnull) i
I have following table structure table domains ID | name | ________________________ 1 |
I have following table: <table class=AMDetailTableCss id=tblQueueHistory cellspacing=5 cellpadding=5 > <tr> <td id=Working> <div
I have following table Id | Status | date ------------------- 1 | Onsite |2007
I have the following table in my database Membership ComapanyId IsMember JoinedDate ExpiryDate RejoinDate
I have this following table (I'm thinking of putting this as a datatable ,

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.