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 7397861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:48:03+00:00 2026-05-29T03:48:03+00:00

Let’s assume we have this very simple table: |class |student| ————— Math Alice Math

  • 0

Let’s assume we have this very simple table:

|class   |student|
---------------
 Math     Alice
 Math     Bob
 Math     Peter
 Math     Anne
 Music    Bob
 Music    Chis
 Music    Debbie
 Music    Emily
 Music    David
 Sports   Alice
 Sports   Chris
 Sports   Emily
 .
 .
 .

Now I want to find out, who I have the most classes in common with.
So basically I want a query that gets as input a list of classes (some subset of all classes)
and returns a list like:

|student |common classes|
 Brad     6
 Melissa  4
 Chris    3
 Bob      3
 .
 .
 .

What I’m doing right now is a single query for every class. Merging the results is done on the client side. This is very slow, because I am a very hardworking student and I’m attending around 1000 classes – and so do most of the other students. I’d like to reduce the transactions and do the processing on the server side using stored procedures. I have never worked with sprocs, so I’d be glad if someone could give me some hints on how to do that.

(note: I’m using a MySQL cluster, because it’s a very big school with 1 million classes and several million students)

UPDATE

Ok, it’s obvious that I’m not a DB expert 😉 4 times the nearly the same answer means it’s too easy.
Thank you anyway! I tested the following SQL statement and it’s returning what I need, although it is very slow on the cluster (but that will be another question, I guess).

SELECT student, COUNT(class) as common_classes
FROM classes_table
WHERE class in (my_subject_list)
GROUP BY student
ORDER BY common_classes DESC

But actually I simplified my problem a bit too much, so let’s make a bit it harder:

Some classes are more important than others, so they are weighted:

| class   | importance |
  Music     0.8
  Math      0.7
  Sports    0.01
  English   0.5
  ...

Additionally, students can be more ore less important.
(In case you’re wondering what this is all about… it’s an analogy. And it’s getting worse. So please just accept that fact. It has to do with normalizing.)

|student | importance |
 Bob       3.5
 Anne      4.2
 Chris     0.3
 ...

This means a simple COUNT() won’t do it anymore.
In order to find out who I have the most in common with, I want to do the following:

map<Student,float> studentRanking;
foreach (Class c in myClasses)
{
     float myScoreForClassC = getMyScoreForClass(c);
     List students = getStudentsAttendingClass(c);
     foreach (Student s in students)
     {
          float studentScoreForClassC = c.classImportance*s.Importance;
          studentRanking[s] += min(studentScoreForClassC, myScoreForClassC);
     }
}

I hope it’s not getting too confusing.

I should also mention that I myself am not in the database, so I have to tell the SELECT statement / stored procedure, which classes I’m attending.

  • 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-05-29T03:48:03+00:00Added an answer on May 29, 2026 at 3:48 am
    SELECT student, COUNT(class) as common_classes
    FROM classes_table
    WHERE class in (my_subject_list)
    GROUP BY student
    ORDER BY common_classes DESC
    

    Update re your question update.

    Assuming there’s a table class_importance and student_importance as you describe above:

    SELECT classes.student, SUM(ci.importance*si.importance) AS weighted_importance
    FROM classes
    LEFT JOIN class_importance ci ON classes.class=ci.class
    LEFT JOIN student_importance si ON classes.student=si.student
    WHERE classes.class in (my_subject_list)
    GROUP BY classes.student
    ORDER BY weighted_importance DESC
    

    The only thing this doesn’t have is the LEAST(weighted_importance, myScoreForClassC) because I don’t know how you calculate that.

    Supposing you have another table myScores:

    class | score
    Math      10
    Sports     0
    Music     0.8
    ...
    

    You can combine it all like this (see the extra LEAST inside the SUM):

    SELECT classes.student, SUM(LEAST(m.score,ci.importance*si.importance)) -- min
                             AS weighted_importance
    FROM classes
    LEFT JOIN class_importance ci ON classes.class=ci.class
    LEFT JOIN student_importance si ON classes.student=si.student
    LEFT JOIN myScores m ON classes.class=m.class                 -- add in myScores
    WHERE classes.class in (my_subject_list)
    GROUP BY classes.student
    ORDER BY weighted_importance DESC
    

    If your myScores didn’t have a score for a particular class and you wanted to assign some default, you could use IFNULL(m.score,defaultvalue).

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

Sidebar

Related Questions

Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have table with column 'URL' whrere I store urls like this
Let give you the details of this rather odd issue. We have a very
Let's say that I have classes like this: public class Parent { public int
Let's say I have a simple UserControl with no codebehind: <UserControl xmlns= .... x:Class=TrafficLight.LightControl>
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>

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.