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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:40:23+00:00 2026-05-22T22:40:23+00:00

I have 2 select statements and would like to combine them so they’re displayed

  • 0

I have 2 select statements and would like to combine them so they’re displayed in 1 table.

Select 1:

SELECT
LAST_DAY, SUM(BILLABLE), AVG(BILLABLE)
FROM EMPLOYEES
GROUP BY YEAR(LAST_DAY),WEEK(LAST_DAY) ORDER BY LAST_DAY

Select 2:

SELECT BILLABLE 
FROM EMPLOYEES 
WHERE NAME LIKE '$lookup%'

The goal here is to show all employee’s billable sum and average per week (the first select statement) and then add a column with the particular ‘lookup’ employee’s billable amount for that week.

Both statements work fine individually, but it would be more meaningful for the employee if they could easily compare where they stand on a weekly basis vs the average.

Edit for an example:

The EMPLOYEE table looks something like this

NAME | LAST_DAY | BILLABLE
Bob   05/13/2011   18.5
Mary  05/13/2011   12.68
Steve 05/13/2011   15.2
Bob   05/06/2011   14.1
Mary  05/06/2011   11.17
Steve 05/06/2011   23.62

I want the results of the query to look something like this:
Assuming $lookup == “Bob”

LAST_DAY   | ALL_BILLABLE_TOTAL | ALL_BILLABLE_AVG | EMPLOYEE_BILLABLE_TOTAL
05/13/2011     46.38                15.46                 18.5
05/06/2011     48.89                16.29                 14.1

  • 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-22T22:40:24+00:00Added an answer on May 22, 2026 at 10:40 pm

    The “lookup” row thing looks like it’s probably a kludge, so I’d question your design. Also, it looks like your “Employee” table isn’t actually rows of employees, but rows of weekly sales or something. It doesn’t seem like it’s 3NF, which will probably cause you a lot of headaches. Anyway, I think that this will do what you want:

    SELECT
        SQ.last_day,
        SQ.all_billable_total,
        SQ.avg_billable_total,
        LU.billable AS employee_billable_total
    FROM
        (
            SELECT
                E.last_day,
                SUM(E.billable) AS all_billable_total,
                AVG(E.billable) AS avg_billable_total,
            FROM
                Employees
            WHERE
                name NOT LIKE '$lookup%'
            GROUP BY
                E.last_day
        ) SQ
    LEFT OUTER JOIN Employees LU ON
        LU.name LIKE '$lookup%' AND
        LU.last_day = SQ.last_day
    

    The join to the lookup row will cause multiple results if you have multiple rows that match the join. If this is just a row to tally that week’s sales, then get rid of those rows and do something like this:

    SELECT
        E.last_day,
        SUM(CASE WHEN name = @name THEN E.billable ELSE NULL END) AS all_billable_total,
        AVG(CASE WHEN name = @name THEN E.billable ELSE NULL END) AS avg_billable_total,
        SUM(E.billable) AS employee_billable_total
    FROM
        Employees E
    WHERE
        E.last_day IN (SELECT last_day FROM Employees WHERE name = @name)
    GROUP BY
        E.last_day
    

    You need to use the NULL in the CASE statements because using 0 will throw off the average.

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

Sidebar

Related Questions

I have 2 seperate select statements, using aggregate functions in each. I would like
I would like to combine these 2 statements but can't seem to get it
I have the trigger in one table and would like to read UserId value
Let's suppose I have three different select statements, like this SELECT 'Final', ID FROM
I currently have two SQL statements that I'd like to combine into one, and
I have a table that contains some sensitive data that I would like to
I have tried the following two statements: SELECT col FROM db.tbl WHERE col (LIKE
I basically have 7 select statements that I need to have the results output
I have a stored procedure with multiple insert/select statements. Let's say I'm using the
I am trying to learn MySQL select statements a little better and I have

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.