I’m looking to take the total time a user worked on each batch at his workstation, the total estimated work that was completed, the amount the user was paid, and how many failures the user has had for each day this year. If I can join all of this into one query then I can use it in excel and format things nicely in pivot tables and such.
EDIT: I’ve realized that is only possible to do this in multiple queries so I have narrowed my scope down to this:
SELECT batch_log.userid,
batches.operation_id,
SUM(TIME_TO_SEC(ramses.batch_log.time_elapsed)),
SUM(ramses.tasks.estimated_nonrecurring + ramses.tasks.estimated_recurring),
DATE(start_time)
FROM batch_log
JOIN batches ON batch_log.batch_id=batches.id
JOIN ramses.tasks ON ramses.batch_log.batch_id=ramses.tasks.batch_id
JOIN protocase.tblusers on ramses.batch_log.userid = protocase.tblusers.userid
WHERE DATE(ramses.batch_log.start_time) > "2011-01-01"
AND protocase.tblusers.active = 1
GROUP BY userid, batches.operation_id, start_time
ORDER BY start_time, userid ASC
The cross join was causing the problem.
No, in general a
Havingclause is used to filter the results of yourGroup by– for example, only reporting those who were paid for more than 24 hours in a day (HAVING SUM(ramses.timesheet_detail.paidTime) > 24). Unless you need to perform filtering of aggregate results, you shouldn’t need ahavingclause at all.Most of those conditions should be moved into a
whereclause, or as part of the joins, for two reasons – 1) Filtering should in general be done as soon as possible, to limit the work the query needs to perform. 2) If the filtering is already done, restating it may cause the query to perform additional, unneeded work.From what I’ve seen so far, it appears that you’re trying to roll things up by the day – try changing the last column in the
group byclause todate(ramses.batch_log.start_time), or you’re grouping by (what I assume is) a timestamp.EDIT:
About schema names – yes, you can name them in the
fromandjoinsections. Often, too, the query may be able to resolve the needed schemas based on some default search list (how or if this is set up depends on your database).Here is how I would have reformatted the query:
Of particular concern is the
batch_log.start_time = timesheet_detail.for_dayline, which is comparing (what are implied to be) timestamps. Are these really equal? I expect that one or both of these should be wrapped in adate()function.As for why you may be getting unexpected data – you appear to have eliminated some of your join conditions. Without knowing the exact setup and use of your database, I cannot give the exact reason for your results (or even able to say they are wrong), but I think the fact that you join to the
operationstable without anyjoincondition is probably to blame – if there are 2 records in that table, it will double all of your previous results, and it looks like there may be 12. You also removedoperations.namefrom thegroup byclause, which may or may not give you the results you want. I would look into the rest of your table relationships, and see if there are any further restrictions that need to be made.