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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:43:08+00:00 2026-05-24T00:43:08+00:00

I currently have quite a messy query, which joins data from multiple tables involving

  • 0

I currently have quite a messy query, which joins data from multiple tables involving two subqueries. I now have a requirement to group this data by DAY(), WEEK(), MONTH(), and QUARTER().

I have three tables: days, qos and employees. An employee is self-explanatory, a day is a summary of an employee’s performance on a given day, and qos is a random quality inspection, which can be performed many times a day.

At the moment, I am selecting all employees, and LEFT JOINing day and qos, which works well. However, now, I need to group the data in order to breakdown a team or individual’s performance over a date range.

Taking this data:

Employee

id  | name
------------------
1   | Bob Smith

Day

id  | employee_id  | day_date   | calls_taken
---------------------------------------------
1   | 1            | 2011-03-01 | 41
2   | 1            | 2011-03-02 | 24
3   | 1            | 2011-04-01 | 35

Qos

id   | employee_id  | qos_date   | score
----------------------------------------
1    | 1            | 2011-03-03 | 85
2    | 1            | 2011-03-03 | 95
3    | 1            | 2011-04-01 | 91

If I were to start by grouping by DAY(), I would need to see the following results:

Day__date  | Day__Employee__id | Day__calls | Day__qos_score
------------------------------------------------------------
2011-03-01 | 1                 | 41         | NULL
2011-03-02 | 1                 | 24         | NULL
2011-03-03 | 1                 | NULL       | 90
2011-04-01 | 1                 | 35         | 91

As you see, Day__calls should be SUM(calls_taken) and Day__qos_score is AVG(score). I’ve tried using a similar method as above, but as the date isn’t known until one of the tables has been joined, its only displaying a record where there’s a day saved.

Is there any way of doing this, or am I going about things the wrong way?

Edit: As requested, here’s what I’ve come up with so far. However, it only shows dates where there’s a day.

SELECT COALESCE(`day`.day_date, qos.qos_date)      AS Day__date,
      employee.id                                  AS Day__Employee__id,
      `day`.calls_taken                            AS Day__Day__calls,
      qos.score                                    AS Day__Qos__score
FROM   faults_employees `employee`
      LEFT JOIN (SELECT `day`.employee_id                    AS employee_id,
                        SUM(`day`.calls_taken)               AS `calls_in`,
                 FROM   faults_days AS `day`
                 WHERE employee.id = 7
                 GROUP  BY (`day`.day_date)
        ) AS `day`
        ON `day`.employee_id = `employee`.id
      LEFT JOIN (SELECT `qos`.employee_id AS employee_id,
                        AVG(qos.score)   AS `score`
                 FROM   faults_qos qos
                 WHERE employee.id = 7
                 GROUP  BY (qos.qos_date)
        ) AS `qos`
        ON `qos`.employee_id = `employee`.id AND `qos`.qos_date = `day`.day_date
WHERE employee.id = 7
GROUP  BY Day__date
ORDER BY `day`.day_date ASC
  • 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-24T00:43:08+00:00Added an answer on May 24, 2026 at 12:43 am

    The solution I’m comming up with looks like:

    SELECT
        `date`,
        `employee_id`,
        SUM(`union`.`calls_taken`) AS `calls_taken`,
        AVG(`union`.`score`) AS `score`
    FROM ( -- select from union table
        (SELECT -- first select all calls taken, leaving qos_score null
            `day`.`day_date` AS `date`,
            `day`.`employee_id`,
            `day`.`calls_taken`,
            NULL AS `score`
        FROM `employee`
        LEFT JOIN
            `day`
                ON `day`.`employee_id` = `employee`.`id`
        )
        UNION -- union both tables
        (
        SELECT -- now select qos score, leaving calls taken null
            `qos`.`qos_date` AS `date`,
            `qos`.`employee_id`,
            NULL AS `calls_taken`,
            `qos`.`score`
        FROM `employee`
        LEFT JOIN
            `qos`
                ON `qos`.`employee_id` = `employee`.`id`
        )
    ) `union`
    GROUP BY `union`.`date` -- group union table by date
    

    For the UNION to work, we have to set the qos_score field in the day table and the calls_taken field in the qos table to null. If we don’t, both calls_taken and score would be selected into the same column by the UNION statement.
    After this, I selected the required fields with the aggregation functions SUM() and AVG() from the union’d table, grouping by the date field in the union table.

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

Sidebar

Related Questions

I currently have two tables, one for pages (objects) and one for images (media).
I am developing a webpage at http://www.knowledgenation.us and currently I have roughly 500 posts
Currently, I am working on system that does quite a bit of reporting-style functions
I currently use the rangeinput plugin from jQuery Tools in various places. But I
I have a process that imports a daily file of product registrations, and adds
Im not really sure what im looking for but currently in my system i
As many of you know, there are quite a few websites that sell royalty
Can I use a SQL Server Express database as my local database for an
I am trying to build an app where I let my registered user be
I am trying to get involved in XNA game development (most likely for Windows

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.