Select Sum on two fields using 2 key fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KaliKraft
    New Member
    • Nov 2007
    • 7

    Select Sum on two fields using 2 key fields

    I have a tblWork that has a dozen fields in it.
    It tracks hours worked for 2 employees at a time.
    - fldDriverID
    - fldDriverHours
    - fldHelperID
    - fldHelperHours

    It also has a date field (fldDate) and a key to the ticket the work was performed on (fldTicketID).

    So I assign the driver and the hours worked (1,15h) same for helper (1,13h)

    What i want to do is do a payroll select that will get me the DriverHours and HelperHours for each employee. that is easy to do but the hard part is that the driver can be the helper and viseversa.
    *
    SampleData:

    Code:
      fldTicketID        1         2         3         4         5
    fldDate        2007-2-1  2007-2-1  2007-2-1  2007-2-2  2007-2-3
    fldDriverID        10       11        10        11        13
    fldDriverHours     8        8         8        7.5         8
    fldHelperID        11       10        13        13        10
    fldHelperHours     8        7.5        8         8          8
    Expected Results

    DateRange = 2007-2-1 -> 2007-2-3
    Code:
    EmployeeID           10            11          13
    DrivenHours           16           15.5         8
    HelperHours          15.5           8           16
    Any Ideas?

    What I tried, got close

    [CODE=mysql]select
    (select sum(fldDriverHo urs)
    from tblWork
    where tblWork.fldTick etID = t.fldTicketID and fldDriverID = ?EmployeeID) as DrivenHours,
    (select sum(fldHelperHo urs)
    from tblWork
    where tblWork.fldTick etID = t.fldTicketID and fldHelperID = ?EmployeeID) as SwampHours
    from tblWork w inner join tblTicket t on t.fldTicketID = w.fldTicketID
    where t.fldDate >= '2007-10-10' and t.fldDate <= '2007-10-13'[/CODE]
    Last edited by mwasif; Nov 9 '07, 04:50 PM. Reason: Formatted query
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Wlecome to TSDN!

    What you have made so far?

    Comment

    • KaliKraft
      New Member
      • Nov 2007
      • 7

      #3
      Originally posted by mwasif
      Wlecome to TSDN!

      What you have made so far?
      I edited my original post with what i have so far.

      Currently I am doing the select shown over and over again for each employee (takes time, especially once they do it for 30+ employees), I would like to get it as one select with the results.

      Thanks,
      Jordon

      Comment

      • mwasif
        Recognized Expert Contributor
        • Jul 2006
        • 802

        #4
        Originally posted by KaliKraft
        What I tried, got close
        Your query giving the required results, right?

        To get multiple results, replace the following condition with a JOIN to employee table and in WHERE clause provide the employee ids.
        Code:
        fldDriverID = ?EmployeeID

        Comment

        • KaliKraft
          New Member
          • Nov 2007
          • 7

          #5
          **Sorry for late reply, i didn't get notified by email**

          Originally posted by mwasif
          Your query giving the required results, right?
          No My query is only giving the results of the employee of the ID that I send in as a parameter.

          Originally posted by mwasif
          To get multiple results, replace the following condition with a JOIN to employee table and in WHERE clause provide the employee ids.
          Code:
          fldDriverID = ?EmployeeID
          I can't do that. See a employeeId is linked to a DriverID or a HelperID or both.

          The result i get from a join are way wrong.

          I will include the table structure:

          tblEmployee
          - fldEmployeeID
          - fldName

          tblTicket
          - fldTicketID
          - fldDate

          tblWork
          - fldWorkID
          - fldTicketID
          - fldDriverID (fldEmployeeID)
          - fldHelperID (fldEmployeeID)
          - fldDriverHours
          - fldHelperHours

          Thanks,
          Jordon

          Comment

          • KaliKraft
            New Member
            • Nov 2007
            • 7

            #6
            I have tried a few more things and now I have a select that returns all the hours for all the employees.

            Code:
            select w.fldDriverHours, w.flddriverid, w.fldHelperHours, w.fldHelperid,t.flddate
            From tblTicket t inner join tblWork w on w.fldTicketID = t.fldTicketID
            where fldDate >= '2007-10-10' AND fldDate <= '2007-10-13'
            I get these results:

            Code:
            15.00, 4, 15.00, 5, '2007-10-10'
            7.00,  4, 8.50,  5, '2007-10-10'
            6.35,  6, 7.42,  6, '2007-10-12'
            7.00,  4, 6.00,  6, '2007-10-12'
            How do i group this in a way so i know total hours for each employee for what task they did? ( I wish i could change the database set up but i can't)

            Expected Results:
            Code:
            4,   29,     NULL
            5,   NULL,   23.5
            6,   6.35,   13.42

            Thanks,
            Jordon
            Last edited by KaliKraft; Nov 16 '07, 11:10 PM. Reason: Added expected results

            Comment

            • pradeep kaltari
              Recognized Expert New Member
              • May 2007
              • 102

              #7
              Originally posted by KaliKraft
              Expected Results:
              Code:
              4,   29,     NULL
              5,   NULL,   23.5
              6,   6.35,   13.42

              Thanks,
              Jordon
              Hi Jordon,
              You can try something like the following:
              [code=mysql]
              select a.empid employee, driving, helping
              from (select empid, sum(driver_hrs) driving from emp left outer join work on empid=driver group by empid) a join
              (select empid, sum(helper_hrs) helping from emp left outer join work on empid=helper group by empid) b on a.empid=b.empid
              [/code]

              plz change the code to match your tables
              Hope this helps.

              -Pradeep

              Comment

              • KaliKraft
                New Member
                • Nov 2007
                • 7

                #8
                Originally posted by pradeep kaltari
                Hi Jordon,
                You can try something like the following:
                [code=mysql]
                select a.empid employee, driving, helping
                from (select empid, sum(driver_hrs) driving from emp left outer join work on empid=driver group by empid) a join
                (select empid, sum(helper_hrs) helping from emp left outer join work on empid=helper group by empid) b on a.empid=b.empid
                [/code]

                plz change the code to match your tables
                Hope this helps.

                -Pradeep

                Thank you it works great.

                Now I need to add a wrench.

                I have to filter it using a date range like my first post. On the "work" table there is a date that i need to filter by. Either for a date or a date range or for a year.

                Hopefully i can edit it. I will reply if i have any issues.

                Thank you,
                Jordon

                Comment

                • KaliKraft
                  New Member
                  • Nov 2007
                  • 7

                  #9
                  Originally posted by KaliKraft
                  Thank you it works great.

                  Now I need to add a wrench.

                  I have to filter it using a date range like my first post. On the "work" table there is a date that i need to filter by. Either for a date or a date range or for a year.

                  Hopefully i can edit it. I will reply if i have any issues.

                  Thank you,
                  Jordon

                  This is what I came up with:

                  [code=mysql]
                  SELECT a.empid employee, driving, helping
                  FROM (SELECT empid,fldTicket ID, sum(driver_hrs) driving FROM emp LEFT OUTER JOIN work on empid=driver GROUP BY empid) a JOIN
                  (SELECT empid,fldTicket ID, sum(helper_hrs) helping FROM emp LEFT OUTER JOIN work on empid=helper GROUP BY empid) b on a.empid=b.empid
                  inner join tblTicket t on a.fldTicketID = t.fldticketID or b.fldTicketID = t.fldticketID
                  where t.fldDate =>= '2007-10-10' and t.fldDate <= '2007-10-12'
                  [/code]

                  What do you think?
                  Last edited by KaliKraft; Nov 20 '07, 04:57 PM. Reason: Premaute Posting :P

                  Comment

                  • pradeep kaltari
                    Recognized Expert New Member
                    • May 2007
                    • 102

                    #10
                    Originally posted by KaliKraft
                    This is what I came up with:

                    [code=mysql]
                    SELECT a.empid employee, driving, helping
                    FROM (SELECT empid,fldTicket ID, sum(driver_hrs) driving FROM emp LEFT OUTER JOIN work on empid=driver GROUP BY empid) a JOIN
                    (SELECT empid,fldTicket ID, sum(helper_hrs) helping FROM emp LEFT OUTER JOIN work on empid=helper GROUP BY empid) b on a.empid=b.empid
                    inner join tblTicket t on a.fldTicketID = t.fldticketID or b.fldTicketID = t.fldticketID
                    where t.fldDate =>= '2007-10-10' and t.fldDate <= '2007-10-12'
                    [/code]

                    What do you think?
                    Hi KaliKraft,
                    I guess your query should work.

                    You can also use:
                    [code=mysql]
                    SELECT a.empid employee, driving, helping
                    FROM (SELECT empid,fldTicket ID, sum(driver_hrs) driving FROM emp LEFT OUTER JOIN work on empid=driver GROUP BY empid) a JOIN
                    (SELECT empid,fldTicket ID, sum(helper_hrs) helping FROM emp LEFT OUTER JOIN work on empid=helper GROUP BY empid) b on a.empid=b.empid
                    inner join tblTicket t on a.fldTicketID = t.fldticketID and t.fldDate >= '2007-10-10' and t.fldDate <= '2007-10-12'
                    [/code]

                    - Pradeep

                    Comment

                    • KaliKraft
                      New Member
                      • Nov 2007
                      • 7

                      #11
                      WRENCH #3

                      Than you for all your help so far but now i would like to get these types of results.

                      Sample Data:
                      Code:
                      15.00, 4, 15.00, 5, '2007-10-10'
                      7.00,  4, 8.50,  5, '2007-10-10'
                      6.35,  6, 7.42,  6, '2007-10-12'
                      7.00,  4, 6.00,  6, '2007-10-12'
                      Results:
                      Code:
                      4, 22.00, null,  '2007-10-10'
                      4, 7.00,  null,  '2007-10-12'
                      5, null,  23.50, '2007-10-10'
                      6, 6.35,  13.42, '2007-10-12'
                      I get the sample data by using this select:

                      [code=sql]
                      select fldDriverHours, fldDriverID, fldHelperHours, fldHelperId, fldDate
                      from tblWork inner join tblTicket on tblWork.fldTick etID = tblTicket.fldTi cketID
                      order by fldDate
                      [/code]

                      Thanks,
                      Jordon

                      Comment

                      Working...