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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:51:46+00:00 2026-06-16T03:51:46+00:00

I’m looking to compute a column based on two other columns, but I have

  • 0

I’m looking to compute a column based on two other columns, but I have some special cases where the computation has to take multiple rows into account.

Here’s the query as it stands now:

SELECT
    UserName
    , EntryDate
    , Project
    , HoursWorked
    , HoursAvailable
    , UtilPct as HoursWorked / HoursAvailable
FROM
    MyDatabase
ORDER BY
    EntryDate

Which results in:


UserName EntryDate Project HoursWorked HoursAvailable UtilPct
Justin 12/17/2012 ABC 8 8 100
Justin 12/18/2012 ABC 4 8 50
Justin 12/18/2012 DEF 4 8 50

But 50% utilization for the last two entries is wrong, because both happened on the same day. Both of those rows should show 100%.

I need the query to…

  1. Sum up all of the HoursWorked for a certain day
  2. Divide that by the max HoursAvailable for that day
  3. Put that value into the UtilPct column for each row

How do I make it do that?

  • 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-06-16T03:51:47+00:00Added an answer on June 16, 2026 at 3:51 am

    I need the query to…

    Sum up all of the HoursWorked for a certain day
    Divide that by the max HoursAvailable for that day
    Put that value into the UtilPct column for each row

    How do I make it do that?

    Sum up all of the HoursWorked:

    SUM(HoursWorked) ... GROUP BY EntryDate
    

    Divide that by the max HoursAvailable for that day

    SUM(HoursWorked)/MAX(HoursAvailable) ... GROUP BY EntryDate
    

    Put that value into the UtilPct column for each row:

    SELECT ... SUM(HoursWorked)/MAX(HoursAvailable)*100.0 AS UtilPct
        FROM MyDatabase
        GROUP BY EntryDate
        ORDER BY EntryDate
    

    But now you won’t be able to divide the rows by Project, you will only see the time utilization any single day. If you try to GROUP BY Project, you will get 50% instead of 100% again.

    That is because you are asking for 4/8 to be 100%, and that would get you semantically nonsensical answers, such as “On Tuesday, I worked 100% on project ABC, and also 100% on project DEF”, to which one could answer “So on Tuesdays there’s 200% of you?”

    You can solve the quandary using a JOIN between the table and itself to get the TotalHoursWorked in a single day, which is semantically different from the HoursWorked on a single project:

    SELECT
        UserName
        , md1.EntryDate
        , Project
        , HoursWorked
        , HoursAvailable
        , (TotalHoursWorked / HoursAvailable)*100.0 AS UtilPct
    FROM
        MyDatabase AS md1
        JOIN ( 
            SELECT EntryDate, SUM(HoursWorked) AS TotalHoursWorked
            FROM MyDatabase GROUP BY EntryDate
        ) AS md2 ON (md1.EntryDate = md2.EntryDate)
    ORDER BY
        md1.EntryDate
    

    Now the UtilPct will refer to the time usage of that day, which means you could get a 100% value on a project even if you worked at it for five minutes only, provided those five minutes were all that you had available that day. And you will still be able to add another column to indicate the percentage of HoursWorked over HoursAvailable for that project that day (in most systems, it ought to be roughly proportional to that project’s priority).

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.