I need to workout the effectiveness of a user to display in a report, i complied the following statement
SELECT COUNT(`call_id`) AS logged
FROM `tbl_calls`
WHERE `user_id_attended_by` = 24
AND YEAR(`date_ack_by_tech`) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)
AND MONTH(`date_ack_by_tech`) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
AND `fk_supplier_id` = 3
UNION ALL
SELECT COUNT(call_id) AS ACK
FROM `tbl_calls`
WHERE YEAR(`date_logged`) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)
AND MONTH(`date_logged`) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
AND `repaired_by` = 24
AND `fk_supplier_id` = 3
The output for the above motioned statement as follows,
I need to get the percentages of the two totals in the pictures to add it to a report to get the users performance, I tried to create a temp table and entering the data into it but could not get it working in xampp.
2
Answers
?
I have no idea what the difference is between
date_ack_by_tech
anddate_logged
, but I assume you have a reason for using them. So, I have not tried to smudge your queries together. If there is no need to differentiate between these two dates, then Akina’s method is preferable.I would drop the use of MONTH() and YEAR() and instead use a date range. This will make these criteria sargable, which may or may not be helpful in this case. But it also does away with your current January issue (Jan 2023 returns Dec 2022, whereas Feb 2023 returns Jan 2022 – 1 month or 13 months).
If you want to continue using your MONTH() and YEAR() method, you should change the year criterion to: