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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:29:17+00:00 2026-05-26T05:29:17+00:00

I am currently developing a database system for a school that will work with

  • 0

I am currently developing a database system for a school that will work with our booking system so students can book their courses and we can better track their activity. Right now I am faced with a complex issue, what I want to do is check which student contracts are valid, I can do so by using the following function, but I want to see if there is an easier way to properly do it (besides storing data I can compute in the system.)

If a contract is valid then the student has not used up all the hours they have purchased, hours purchased is the sum of the length of teach class they have attended added to the sum of the lengths of each class they missed but booked (after a certain number, such as 3 or 5). I can do it with the following query but I feel like there must be a simpler way:

SELECT
    level.name
FROM
    (
        SELECT
            contract.level_package_id,
            contract_class_hours.hours_purchased,
            (
                SELECT
                    isnull(sum(DATEPART(hh, class.end_date - class.start_date)), 0)
                FROM
                    booking
                    JOIN class ON class.id = booking.class_id
                WHERE
                    booking.booking_state_id = 3
                    AND booking.contract_id = contract.id
            ) AS time_attended,
            (
                SELECT 
                    isnull(sum(absent_class_lengths.length), 0)
                FROM
                    (
                        SELECT
                            DATEPART(hh, class.end_date - class.start_date) AS length,
                            row_number() OVER (ORDER BY class.start_date) AS rn
                        FROM
                            booking
                            JOIN class ON class.id = booking.class_id
                        WHERE
                            student_id = 5
                            AND booking_state_id = 4
                            AND booking.contract_id = contract.id
                    ) absent_class_lengths
                WHERE 
                    rn > contract_class_hours.absences_allowed
            ) as time_absent
        FROM
            contract
            JOIN contract_class_hours ON contract_class_hours.contract_id = contract.id
    ) test
    JOIN level_package_level ON level_package_level.level_package_id = test.level_package_id
    JOIN level ON level.id = level_package_level.level_id
WHERE
    test.time_absent + test.time_attended < test.hours_purchased
    AND level.study_type_id = 2
  • booking.state_id = 3 means the student attended class
  • booking.state_id = 4 means the student was absent
  • level.study_type_id = 2 is just a course subject

The tables contain data columns like these (ignore level_id, it is just a value I need to return):

CLASS
    id - int
    end_date - datetime
    start_date - datetime

BOOKING
    id - int
    class_id - int
    student_id - int
    booking_state_id - smallint

BOOKING_STATE
    id - int
    state - varchar(20) [absent, attended]

CONTRACT
    id - int
    student_id - int
    level_id - int
    hours_purchased - smallint
    absenses_allowed - smallint

STUDENT
    id - int

I realize this may be to complicated a question to ask, but I am just wondering if this really is the proper way to do things, or if I should just save some sort of field in the contract table that has a number of hours attended and assume it is always accurate.

  • 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-26T05:29:18+00:00Added an answer on May 26, 2026 at 5:29 am

    Seems like you could do a few things to improve the situation

    1. Create a view with some of the code
    2. Use the above response to create a stored procedure, and make the hard coded ids variables
      WHERE class.student_id = 5
      AND booking_state_id = 4

    I am guessing these are not always going to be the same and having a stored procedure will allow a plan to be cached as compared to an AD Hoc sql statement. This will allow you to send in various ids to get the results you want and return a data set that could be used in a web applciaiton of simply converted to and Exce or Word table for reporting.

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

Sidebar

Related Questions

Alright, here we go! I am currently developing an iPhone app that will work
I am currently developing a Rails application using a database that was designed before
I'm currently developing a PHP application that's using an Access database as a backend.
I am currently developing an approval routing WCF service that will allow an user
Im currently developing a system where the user will end up having large arrays(
I am currently developing a system that holds all relevant master data like for
Hello i am currently developing a kind of wiki system for my school, this
I am currently developing an event management system where the database stores time slots
I´m looking for a unit tests framework for database development. I´m currently developing for
I'm currently developing an application that is comprised of five separate executables that communicate

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.