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: - 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 -
EmployeeID 10 11 13
-
DrivenHours 16 15.5 8
-
HelperHours 15.5 8 16
-
Any Ideas?
What I tried, got close - select
-
(select sum(fldDriverHours)
-
from tblWork
-
where tblWork.fldTicketID = t.fldTicketID and fldDriverID = ?EmployeeID) as DrivenHours,
-
(select sum(fldHelperHours)
-
from tblWork
-
where tblWork.fldTicketID = 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'
10 1841
Wlecome to TSDN!
What you have made so far?
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
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. - fldDriverID = ?EmployeeID
**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. - 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
I have tried a few more things and now I have a select that returns all the hours for all the employees. -
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: -
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: -
4, 29, NULL
-
5, NULL, 23.5
-
6, 6.35, 13.42
-
Thanks,
Jordon
Expected Results: -
4, 29, NULL
-
5, NULL, 23.5
-
6, 6.35, 13.42
-
Thanks,
Jordon
Hi Jordon,
You can try something like the following: -
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
-
plz change the code to match your tables
Hope this helps.
-Pradeep
Hi Jordon,
You can try something like the following: -
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
-
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
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: -
SELECT a.empid employee, driving, helping
-
FROM (SELECT empid,fldTicketID, sum(driver_hrs) driving FROM emp LEFT OUTER JOIN work on empid=driver GROUP BY empid) a JOIN
-
(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
-
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'
-
What do you think?
This is what I came up with: -
SELECT a.empid employee, driving, helping
-
FROM (SELECT empid,fldTicketID, sum(driver_hrs) driving FROM emp LEFT OUTER JOIN work on empid=driver GROUP BY empid) a JOIN
-
(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
-
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'
-
What do you think?
Hi KaliKraft,
I guess your query should work.
You can also use: -
SELECT a.empid employee, driving, helping
-
FROM (SELECT empid,fldTicketID, sum(driver_hrs) driving FROM emp LEFT OUTER JOIN work on empid=driver GROUP BY empid) a JOIN
-
(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
-
inner join tblTicket t on a.fldTicketID = t.fldticketID and t.fldDate >= '2007-10-10' and t.fldDate <= '2007-10-12'
-
- Pradeep
WRENCH #3
Than you for all your help so far but now i would like to get these types of results.
Sample Data: -
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: -
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: -
select fldDriverHours,fldDriverID, fldHelperHours,fldHelperId, fldDate
-
from tblWork inner join tblTicket on tblWork.fldTicketID = tblTicket.fldTicketID
-
order by fldDate
-
Thanks,
Jordon
Sign in to post your reply or Sign up for a free account.
Similar topics
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:
----------------------------------------------------------...
|
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...
|
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...
|
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...
|
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...
|
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="...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |