473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.   fldTicketID        1         2         3         4         5
  2. fldDate        2007-2-1  2007-2-1  2007-2-1  2007-2-2  2007-2-3
  3. fldDriverID        10       11        10        11        13
  4. fldDriverHours     8        8         8        7.5         8
  5. fldHelperID        11       10        13        13        10
  6. fldHelperHours     8        7.5        8         8          8    
Expected Results

DateRange = 2007-2-1 -> 2007-2-3
Expand|Select|Wrap|Line Numbers
  1. EmployeeID           10            11          13
  2. DrivenHours           16           15.5         8
  3. HelperHours          15.5           8           16
  4.  
Any Ideas?

What I tried, got close

Expand|Select|Wrap|Line Numbers
  1. select
  2.   (select sum(fldDriverHours)
  3.     from tblWork
  4.     where tblWork.fldTicketID = t.fldTicketID and fldDriverID = ?EmployeeID) as DrivenHours,
  5.   (select sum(fldHelperHours)
  6.     from tblWork
  7.     where tblWork.fldTicketID = t.fldTicketID and fldHelperID = ?EmployeeID) as SwampHours
  8. from tblWork w inner join tblTicket t on t.fldTicketID = w.fldTicketID
  9. where t.fldDate >= '2007-10-10' and t.fldDate <= '2007-10-13'
Nov 9 '07 #1
10 1841
mwasif
802 Expert 512MB
Wlecome to TSDN!

What you have made so far?
Nov 9 '07 #2
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
Nov 9 '07 #3
mwasif
802 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. fldDriverID = ?EmployeeID
Nov 9 '07 #4
**Sorry for late reply, i didn't get notified by email**

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.

To get multiple results, replace the following condition with a JOIN to employee table and in WHERE clause provide the employee ids.
Expand|Select|Wrap|Line Numbers
  1. 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
Nov 16 '07 #5
I have tried a few more things and now I have a select that returns all the hours for all the employees.

Expand|Select|Wrap|Line Numbers
  1. select w.fldDriverHours, w.flddriverid, w.fldHelperHours, w.fldHelperid,t.flddate
  2. From tblTicket t inner join tblWork w on w.fldTicketID = t.fldTicketID
  3. where fldDate >= '2007-10-10' AND fldDate <= '2007-10-13'
I get these results:

Expand|Select|Wrap|Line Numbers
  1. 15.00, 4, 15.00, 5, '2007-10-10'
  2. 7.00,  4, 8.50,  5, '2007-10-10'
  3. 6.35,  6, 7.42,  6, '2007-10-12'
  4. 7.00,  4, 6.00,  6, '2007-10-12'
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. 4,   29,     NULL
  2. 5,   NULL,   23.5
  3. 6,   6.35,   13.42
  4.  

Thanks,
Jordon
Nov 16 '07 #6
pradeep kaltari
102 Expert 100+
Expected Results:
Expand|Select|Wrap|Line Numbers
  1. 4,   29,     NULL
  2. 5,   NULL,   23.5
  3. 6,   6.35,   13.42
  4.  

Thanks,
Jordon
Hi Jordon,
You can try something like the following:
Expand|Select|Wrap|Line Numbers
  1. select a.empid employee, driving, helping
  2. from (select empid, sum(driver_hrs) driving from emp left outer join work on empid=driver group by empid) a join 
  3.      (select empid, sum(helper_hrs) helping from emp left outer join work on empid=helper group by empid) b on a.empid=b.empid
  4.  
plz change the code to match your tables
Hope this helps.

-Pradeep
Nov 20 '07 #7
Hi Jordon,
You can try something like the following:
Expand|Select|Wrap|Line Numbers
  1. select a.empid employee, driving, helping
  2. from (select empid, sum(driver_hrs) driving from emp left outer join work on empid=driver group by empid) a join 
  3.      (select empid, sum(helper_hrs) helping from emp left outer join work on empid=helper group by empid) b on a.empid=b.empid
  4.  
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
Nov 20 '07 #8
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:

Expand|Select|Wrap|Line Numbers
  1.       SELECT a.empid employee, driving, helping
  2.       FROM (SELECT empid,fldTicketID, sum(driver_hrs) driving FROM emp LEFT OUTER   JOIN work on empid=driver GROUP BY empid) a JOIN
  3.            (SELECT empid,fldTicketID, sum(helper_hrs) helping FROM emp LEFT OUTER JOIN work on empid=helper GROUP BY empid) b on a.empid=b.empid
  4. inner join tblTicket t on a.fldTicketID = t.fldticketID or b.fldTicketID = t.fldticketID
  5. where t.fldDate =>= '2007-10-10' and t.fldDate <= '2007-10-12'
  6.  
What do you think?
Nov 20 '07 #9
pradeep kaltari
102 Expert 100+
This is what I came up with:

Expand|Select|Wrap|Line Numbers
  1.       SELECT a.empid employee, driving, helping
  2.       FROM (SELECT empid,fldTicketID, sum(driver_hrs) driving FROM emp LEFT OUTER   JOIN work on empid=driver GROUP BY empid) a JOIN
  3.            (SELECT empid,fldTicketID, sum(helper_hrs) helping FROM emp LEFT OUTER JOIN work on empid=helper GROUP BY empid) b on a.empid=b.empid
  4. inner join tblTicket t on a.fldTicketID = t.fldticketID or b.fldTicketID = t.fldticketID
  5. where t.fldDate =>= '2007-10-10' and t.fldDate <= '2007-10-12'
  6.  
What do you think?
Hi KaliKraft,
I guess your query should work.

You can also use:
Expand|Select|Wrap|Line Numbers
  1.       SELECT a.empid employee, driving, helping
  2.       FROM (SELECT empid,fldTicketID, sum(driver_hrs) driving FROM emp LEFT OUTER   JOIN work on empid=driver GROUP BY empid) a JOIN
  3.            (SELECT empid,fldTicketID, sum(helper_hrs) helping FROM emp LEFT OUTER JOIN work on empid=helper GROUP BY empid) b on a.empid=b.empid
  4. inner join tblTicket t on a.fldTicketID = t.fldticketID and t.fldDate >= '2007-10-10' and t.fldDate <= '2007-10-12'
  5.  
- Pradeep
Nov 21 '07 #10
WRENCH #3

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

Sample Data:
Expand|Select|Wrap|Line Numbers
  1. 15.00, 4, 15.00, 5, '2007-10-10'
  2. 7.00,  4, 8.50,  5, '2007-10-10'
  3. 6.35,  6, 7.42,  6, '2007-10-12'
  4. 7.00,  4, 6.00,  6, '2007-10-12'
  5.  
Results:
Expand|Select|Wrap|Line Numbers
  1. 4, 22.00, null,  '2007-10-10'
  2. 4, 7.00,  null,  '2007-10-12'
  3. 5, null,  23.50, '2007-10-10'
  4. 6, 6.35,  13.42, '2007-10-12'
  5.  
I get the sample data by using this select:

Expand|Select|Wrap|Line Numbers
  1. select fldDriverHours,fldDriverID,   fldHelperHours,fldHelperId, fldDate
  2. from tblWork inner join tblTicket on tblWork.fldTicketID = tblTicket.fldTicketID
  3. order by fldDate
  4.  
Thanks,
Jordon
Nov 29 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: CJM | last post by:
I have a query which produces different results in the Access query builder and in an ASP page (via ADO) An example of the query is: ----------------------------------------------------------...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
9
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
4
by: Ben | last post by:
I believe I am missunderstanding how subqueries work. I simple subquery works fine but when I wish do compare 2 or more fields at once I don't get the results I wish. Table A...
7
by: skeddy | last post by:
In a nutshell, I'm trying to dynamically create a select box with ResultSet code in vbscript and then need to be able to access the value of that select box later with a Save button. I've got...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
6
by: pooh80133 | last post by:
Hi! I am pasting my SQL code at the end of this message. I am trying to use SELECT DISTINCT in a query, but I am a beginner for using Access. Right now I have duplicate ID's (Indiv ID) in my...
4
by: The.Daryl.Lu | last post by:
Hi, Have a bit of a problem... I've created a form in Access and will use the form for a user to query a table based on the selected fields. The problem lies in that I was using checkboxes for...
1
by: bimeldip | last post by:
Hi, I have managed to create codes to display data from the database in a html page.I have gone on to create a page to allow users to manipulate the table via a html page. For instance users will be...
3
by: bpw22az | last post by:
I am currently in the process of setting up an asp page that sends an inquiring student an email regarding his/her application status. The student enters his/her email address on a web page, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.