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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:25:44+00:00 2026-05-16T15:25:44+00:00

I’m currently doing a summer job and I have to extend an existing program.

  • 0

I’m currently doing a summer job and I have to extend an existing program.

My boss asked me to make a tool for our clients so they can see how much their employees cost, per month. But that’s not all. The thing is that a company can have one or more ‘societies’, or subcompanies. We want know how much an employee costs per society in a company.

These are the table I use:

  • society: a subcompany with a people_id which contains the name, etc. of the society
  • timesheet: timesheet entries that contain person and society information
  • people: all people or contacts in the database
  • salarystate: contains the salary for a person for a specific month
  • overhead: overhead cost for a person for a specific month (note that date is a string (!) formatted like this: YYYY-MM-DD)

This query works, but it takes very long time to execute. Is there a way to make it faster?

I select the year and month, get the name of the employee (worker) and the name of the society. Then I select the sum of the minutes the employee has worked (for a specific society). And finally I calculate the cost by checking his salary for that month and the overhead for that month.

SELECT
    YEAR(TS.assigndate) AS timesheet_year,
    MONTH(TS.assigndate) AS timesheet_month,
    CONCAT(TP.name, ' ', TP.firstname) AS worker,
    CONCAT(SP.name, ' ', SP.firstname) AS society,
    (
        SELECT
            SUM(timeunits) AS minutes
        FROM timesheet
        WHERE
            people_id = TP.id AND
            society_id = S.id AND
            MONTH(assigndate) = timesheet_month
    ) AS minutes,
    (
        SELECT (minutes / 60)
    ) AS hours,
    (
        SELECT(OO.hourtarif + SS.hourtarif) AS cost
        FROM salarystate SS, overhead OO
        WHERE
            people_id = TP.id AND
            YEAR(OO.date) = timesheet_year AND
            MONTH(OO.date) = timesheet_month AND
            CONVERT(SUBSTRING(SS.month FROM 1 FOR 4), UNSIGNED) = timesheet_year AND
            CONVERT(SUBSTRING(SS.month, -2), UNSIGNED) = timesheet_month
    ) AS cost,
    (
        SELECT (hours * cost)
    ) AS total_cost
FROM timesheet TS, society S, people SP, people TP
WHERE
    S.id = TS.society_id AND
    SP.id = S.people_id AND
    TP.id = TS.people_id
GROUP BY timesheet_year, timesheet_month, worker, society; 
  • 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-16T15:25:44+00:00Added an answer on May 16, 2026 at 3:25 pm

    Now I use temporary tables it goes fast as hell :). This is the code now, if you’re interested:

    CREATE TEMPORARY TABLE IF NOT EXISTS people_hours (
        people_id INTEGER NOT NULL,
        society_id INTEGER NOT NULL,
        year INTEGER NOT NULL,
        month INTEGER NOT NULL,
        hours DOUBLE NOT NULL,
        PRIMARY KEY(people_id, society_id, year, month)
    );
    
    CREATE TEMPORARY TABLE IF NOT EXISTS people_cost (
        people_id INTEGER NOT NULL,
        year INTEGER NOT NULL,
        month INTEGER NOT NULL,
        cost DOUBLE NOT NULL,
        PRIMARY KEY(people_id, year, month)
    );
    
    TRUNCATE people_hours;
    TRUNCATE people_cost;
    
    INSERT INTO people_hours (people_id, society_id, year, month, hours)
    SELECT
        p.id as people_id,
        s.id as society_id,
        YEAR(t.assigndate) as year,
        MONTH(t.assigndate) as month,
        SUM(t.timeunits)/60 as hours
    FROM people p, society s, timesheet t
    WHERE
        t.society_id = s.id AND
        t.people_id = p.id
    GROUP BY year, month, people_id, society_id;
    
    INSERT INTO people_cost (people_id, year, month, cost)
    SELECT
        p.id as people_id,
        YEAR(o.date) as cost_year,
        MONTH(o.date) as cost_month,
        SUM(o.hourtarif + s.hourtarif) as cost
    FROM people p, salarystate s, overhead o
    WHERE
        s.people_id = p.id AND
        CONVERT(SUBSTRING(s.month FROM 1 FOR 4), UNSIGNED) = YEAR(o.date) AND
        CONVERT(SUBSTRING(s.month, -2), UNSIGNED) = MONTH(o.date)
    GROUP BY cost_year, cost_month, people_id;
    
    SELECT 
        h.year,
        h.month,
        h.society_id,
        h.hours,
        c.cost,
        (h.hours * c.cost) AS total_cost,
        CONCAT(p.name, ' ', p.firstname) AS employee,
        CONCAT(ps.name, ' ', ps.firstname) AS society
    FROM people_hours h, people_cost c, people p, people ps, society s
    WHERE
        h.society_id = s.id AND
        h.people_id = p.id AND
        h.people_id = c.people_id AND
        s.people_id = ps.id AND
        h.year = c.year AND
        h.month = c.month
    ORDER BY h.year, h.month, h.people_id, h.society_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.