So I’ve asked a question like this before, and I guess I just didn’t understand the answer (although the answer worked) to input the answer into this next problem
Easy query. I’m pulling employee, date, totalhrs. out of a timecard program PER employee. What i’m looking for is a sum of totalhrs colum but also to display ALL the timecard’s submitted. So the code I use now
SELECT date,totalhrs,SUM(totalhrs)
FROM tcardsubmit
WHERE employee= 'employee name'
ORDER BY date
This code displays
|EMPLOYEE | DATE | TOTALHRS | SUM(TOTALHRS)
| myname | myfirstdate | 5 | 10
What I want it to display is ALL the timecards that a specific employee has entered. Not JUST the one link. So in ‘myname’ I would have 3 timecards where the SUM does in fact equal 10 hours submitted for the week. The sum works for the math. I just can’t seem to get it to display all the timecards that employee has entered. Here is what I want it to look like
|EMPLOYEE | DATE | TOTALHRS | SUM(TOTALHRS)
| myname | myfirstdate | 5 | 10
| myname | myseconddate | 4 | 10
| myname | mythirddate | 1 | 10
Test this and see if this works for you:
Explanation For where clause in sub select:
In the sub select WHERE clause, we are checking to get just the records that have same employee as the main select employee (b.employee = a.employee) so the SUM(b.totalhrs) would be only sum of records for the employee you are getting his records. In main select we say FROM tcardsubmit a so we name the main tcardsubmit a and in the sub select we name the table b to avoid any conflicts because of using same table and field names.