NorthStarDA
IS-IT--Management
i am writing a query where i need to get a count of transaction types from a table for employees within a certain timeframe. I have a table for work_history with columns time_in and time_out. And there is a transactionHistory table where i want to get my counts. here is an example
problem is, by the time i finish writing those reports i will have to have about 40 different subselects to get all the counts i need and the query is already performing badly - is there a better way to get these numbers based on the info i've given here?
=========================================
Don't sweat the petty things and don't pet the sweaty things.
Code:
SELECT CONCAT(e.last_name, ', ', e.first_name) AS employeeName,th.employeeID,
(SELECT SUM(TIME_TO_SEC(h.time_out)-TIME_TO_SEC(h.time_in)) / 3600
FROM corporate.workHistory h
WHERE h.employee_ID = th.employeeID
AND h.project_ID = 418
AND h.work_date = now()
) totalHours,
(SELECT COUNT(th2.transactionID) FROM transactionHistory th2 WHERE th2.transactionValue = '1' AND th2.transactionTypeID = '1' AND th2.employeeID = e.ID AND date(th2.transactionDate) = CURDATE()) status1,
(SELECT COUNT(th2.transactionID) FROM transactionHistory th2 WHERE th2.transactionValue = '2' AND th2.transactionTypeID = '1' AND th2.employeeID = e.ID AND date(th2.transactionDate) = CURDATE()) status2,
(SELECT COUNT(th2.transactionID) FROM transactionHistory th2 WHERE th2.transactionValue = '3' AND th2.transactionTypeID = '1' AND th2.employeeID = e.ID AND date(th2.transactionDate) = CURDATE()) status3,
(SELECT COUNT(th2.transactionID) FROM transactionHistory th2 WHERE th2.transactionValue = '7' AND th2.transactionTypeID = '1' AND th2.employeeID = e.ID AND date(th2.transactionDate) = CURDATE()) status4,
(SELECT COUNT(th2.transactionID) FROM transactionHistory th2 WHERE th2.transactionValue = '8' AND th2.transactionTypeID = '1' AND th2.employeeID = e.ID AND date(th2.transactionDate) = CURDATE()) status5
FROM transactionHistory th
INNER JOIN corporate.employees e ON e.id = th.employeeID
WHERE date(th.transactionDate) = CURDATE()
GROUP BY th.employeeID
ORDER BY employeeName;
problem is, by the time i finish writing those reports i will have to have about 40 different subselects to get all the counts i need and the query is already performing badly - is there a better way to get these numbers based on the info i've given here?
=========================================
Don't sweat the petty things and don't pet the sweaty things.