Sum up total hours for mutiple day entries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fformulaa
    New Member
    • Mar 2008
    • 1

    Sum up total hours for mutiple day entries

    Can't figure out how to sum up a total of hours from same date entries.

    Query:

    SELECT work_hrs_for_da y.empl_numbr, work_hrs_for_da y.user_date, work_hrs_for_da y.workday_totl, mech_hrs_for_da y.day_totl

    FROM work_hrs_for_da y
    LEFT JOIN mech_hrs_for_da y

    ON (work_hrs_for_d ay.user_date = mech_hrs_for_da y.job_date) AND (work_hrs_for_d ay.empl_numbr = mech_hrs_for_da y.empl_numbr)

    WHERE work_hrs_for_da y.empl_numbr= 699 AND
    work_hrs_for_da y.user_date >= 01/24/2008 AND
    work_hrs_for_da y.user_date >= 02/28/2008 AND
    work_hrs_for_da y.workday_totl > .5 AND
    (abs(work_hrs_f or_day.workday_ totl - mech_hrs_for_da y.day_totl) > .5 OR mech_hrs_for_da y.day_totl is Null)

    ORDER BY user_date


    Result:

    empl_number user_date workday_totl day_totl
    699 1/3/2008 5.03 13.83
    699 1/3/2008 8.8 13.83
    699 1/4/2008 3.95 11.76
    699 1/4/2008 7.81 11.76
    699 1/7/2008 8.17 13.67
    699 1/7/2008 5.5 13.67


    Can't figure out how I can add the date totals so they only show the total of that day!
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    If I understand correctly
    Code:
    SELECT a.empl_numbr, a.user_date, 
           sum(a.workday_totl) as workday_totl, 
           sum(b.day_totl) as day_totl
    FROM work_hrs_for_day a
    LEFT JOIN mech_hrs_for_day b
    ON a.user_date = b.job_date AND a.empl_numbr = b.empl_numbr
    WHERE a.empl_numbr= 699 AND 
          a.user_date >= 01/24/2008 AND 
          a.user_date >=  02/28/2008 AND  
          a.workday_totl > .5 AND 
          abs(a.workday_totl -b.day_totl) > .5 OR a.day_totl is Null
    GROUP BY a.empl_numbr,, a.user_date
    ORDER BY user_date
    I added the group by clause at the end
    and the sum aggregates in the select clause
    I also gave the tables an alias to make the query a bit easier to read
    and I wraped the query In code tags, again to make it easier to read.
    You should do that for your code when asking a question.
    Easy to read for us = more likely to get an answer for you

    Comment

    Working...