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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:08:15+00:00 2026-05-14T23:08:15+00:00

Hope my subject line was descriptive enough. Basically, I have a table called Students,

  • 0

Hope my subject line was descriptive enough.

Basically, I have a table called Students, and one called Absences. The Students table contains student information (Name, DOB, StudentID etc.), the Absences table contains any absences (Date, Reason, related by StudentID). I want to get a full list of the students, plus a count of their absences.

Sample results would be:

StudentName    Absences
-------------  -----------
Johnny         3
Mark           2
James          1
Anthony        0
Sharon         0

Can someone suggest a way I should tackle this problem?

I would prefer to have a single SQL statement, possibly incorporating some views etc.

Thanks,

Jacob.

  • 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-14T23:08:16+00:00Added an answer on May 14, 2026 at 11:08 pm

    This will find the number of absences of each student, and sort with most absences first, as in your example.

    SELECT 
       studentname, count(absences_id)
    FROM
       Students s 
    LEFT OUTER JOIN 
       Absences a ON s.student_id=a.student_id
    GROUP BY studentname
    ORDER BY count(absences_id) DESC, student_name
    

    The LEFT OUTER JOIN is important, since there may be no rows in the absences table for some students. In that case, all fields from Absences for that row will be null, and the count will be zero, since it only counts non-null fields.

    If each absence can span a range of dates, e.g. absences due to sick leave or extended holiday, then you would add start_date, end_date to the Absences table and replace COUNT(absences_id) with SUM(DATEDIFF(day, start_date, end_date)), e.g.

    SELECT 
       studentname, SUM(ISNULL(DATEDIFF(day, start_date, end_date),0)
    FROM
       Students s 
    LEFT OUTER JOIN 
       Absences a ON s.student_id=a.student_id
    GROUP BY studentname
    

    You can set the ORDER BY clause to SUM(…) but it will improve readability and maintainability to wrap it in another query.

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

Sidebar

Related Questions

I happen to have a database with a table that holds all possible combination
I have 8 emails being sent out and 1 webpage where they are linked
I have this little problem I couldn't find in Google: UITableView works fine until
I've been searching high and low for a satisfactory answer to this and failed.
in rails3 i defined a class #coding:utf-8 class CoreMail < ActionMailer::Base def consulting_reply(email) mail(:to
There's this question but it has nothing close to help me out here. Tried
I want to make a program that does an order entry system for beverages.
again a total newbie question from me about Haskell, and Leskah. (First, a subjective
I'll release my first app soon, and I'm afraid of getting dozens of bug
If you use the satellite GMapType using this Google-provided example in v3 of the

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.