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.
SELECT logged,
100*logged/(logged+ack) percent_logged,
ack,
100*ack/(logged+ack) percent_ack
FROM (
SELECT SUM(`user_id_attended_by` = 24) AS logged,
SUM(repaired_by` = 24) AS ack
FROM `tbl_calls`
WHERE 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
) subquery
?
Related
I'm inheriting an SQL query that gets data from my database table based on the number of days before the current date, I'd like to extend this functionality to also include data after the current date, I've tried extending it by adding a new line but my SQL query doesn't return any data, here's the current query:
SELECT
IF((
SELECT
send.id AS "true"
FROM
sends send
WHERE
send.sent = 0 AND
send.subscription_id = subscription.id
LIMIT 1
), true, false) AS already_queued
FROM
subscriptions subscription
WHERE
subscription.unsubscribed = 0 AND
subscription.source_id = 12 AND
DATE(subscription.date_added) <= (CURRENT_DATE - INTERVAL 1 DAY)
ORDER BY
subscription.date_added
DESC
Right now it's the following line that handles the data after a number of days:
DATE(subscription.date_added) <= (CURRENT_DATE - INTERVAL 1 DAY)
Where 1 is replaced dynamically with the value, I now want to add a new line:
DATE(subscription.date_added) >= (CURRENT_DATE - INTERVAL 7 DAY)
So this should return data between the two dates but doesn't, what am I missing from the combined query to make it work:
SELECT
IF((
SELECT
send.id AS "true"
FROM
sends send
WHERE
send.sent = 0 AND
send.subscription_id = subscription.id
LIMIT 1
), true, false) AS already_queued
FROM
subscriptions subscription
WHERE
subscription.unsubscribed = 0 AND
subscription.source_id = 12 AND
DATE(subscription.date_added) >= (CURRENT_DATE - INTERVAL 7 DAY) AND
DATE(subscription.date_added) <= (CURRENT_DATE - INTERVAL 1 DAY)
ORDER BY
subscription.date_added
DESC
We have a table with two fields tkTimeOpen and tkTimeClosed. We need to find the average wait time for this month and last month. I've been unable to get the right SQL query to pull out what I need.
This is how the date-time is recorded; 2017-01-25 10:35
This Month's Average;
SELECT SUM(DATEDIFF(MINUTE,tkTimeOpen,tkTimeClose)) * 1.0
/ (SELECT COUNT(*) * 1.0 FROM e_ticket)
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(tkTimeOpen) = MONTH(CURRENT_DATE - INTERVAL 0 MONTH)
Last Month's Average
SELECT SUM(DATEDIFF(MINUTE,tkTimeOpen,tkTimeClose)) * 1.0
/ (SELECT COUNT(*) * 1.0 FROM e_ticket)
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(tkTimeOpen) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
I've tried a lot of variations but it doesn't give the desired output.
If anyone could help that would be great!
Thank you
This should give you the average minutes for last month and this month combined:
SELECT AVG(TIMESTAMPDIFF(MINUTE, tkTimeOpen, tkTimeClose))
FROM e_ticket
WHERE tkTimeOpen >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY);
If you still need to get the averages separately, you can keep your WHERE clause similar to what you have...
For this month:
SELECT AVG(TIMESTAMPDIFF(MINUTE, tkTimeOpen, tkTimeClose))
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(NOW()) AND MONTH(tkTimeOpen) = MONTH(NOW());
And for last month:
SELECT AVG(TIMESTAMPDIFF(MINUTE, tkTimeOpen, tkTimeClose))
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(NOW() - INTERVAL 1 MONTH) AND MONTH(tkTimeOpen) = MONTH(NOW() - INTERVAL 1 MONTH);
One function you could use is TIMESTAMPDIFF - not DATEDIFF
You can use AVG() instead of SUM() / COUNT()
SELECT AVG(TIMESTAMPDIFF(MINUTE,tkTimeOpen,tkTimeClose))
FROM e_ticket
WHERE YEAR(tkTimeOpen) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(tkTimeOpen) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
I want to write an sql query that must show me records that are only created last month..for example, if this month is June, I want the query to show me records of May.
This is my query but it is not working.
SELECT COUNT(*) AS stdtotal_today FROM `login`
WHERE `login_account_type` = 'STUDENT'
AND `account_created_date` = CURDATE() - 30
Note that login is the table name and account_created_date is the column name of type date.
Try This :
SELECT * FROM `login`
WHERE `login_account_type` = 'STUDENT' AND YEAR(`account_created_date`) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(`account_created_date`) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Try this...
SELECT COUNT(*) AS stdtotal_today FROM `login` WHERE account_created_date
BETWEEN DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01 00:00:00') AND DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH), '%Y-%m-%d 23:59:59')
I have a query that calculates ratios and returns them for each hour and server on a given day:
SELECT a.day,
a.hour,
Sum(a.gemspurchased),
Sum(b.gems),
Sum(b.shadowgems),
( Sum(b.gems) / Sum(a.gemspurchased) ) AS GemRatio,
( Sum(b.shadowgems) / Sum(a.gemspurchased) ) AS ShadowGemRatio
FROM (SELECT Date(Date_sub(createddate, INTERVAL 7 hour)) AS day,
Hour(Date_sub(createddate, INTERVAL 7 hour)) AS hour,
serverid,
Sum(gems) AS GemsPurchased
FROM dollartransactions
WHERE Date(Date_sub(createddate, INTERVAL 7 hour)) BETWEEN
Curdate() - INTERVAL 14 day AND Curdate()
GROUP BY 1,
2,
3) a,
/*Gems recorded from DollarTransactions Table after purchasing gem package*/
(SELECT Date(Date_sub(createddate, INTERVAL 7 hour)) AS day,
Hour(Date_sub(createddate, INTERVAL 7 hour)) AS hour,
serverid,
Sum(acceptedamount) AS Gems,
Sum(acceptedshadowamount) AS ShadowGems
FROM gemtransactions
WHERE Date(Date_sub(createddate, INTERVAL 7 hour)) BETWEEN
Curdate() - INTERVAL 14 day AND Curdate()
AND transactiontype IN ( 990, 2 )
AND fullfilled = 1
AND gemtransactionid >= 130000000
GROUP BY 1,
2,
3) b
/*Gems & Shadow Gems spent, recorded from GemTransactions Table */
WHERE a.day = b.day
AND a.serverid = b.serverid
GROUP BY 1,
2
This code returns the component parts of the ratios, as well as the ratios themselves (which are sometimes null):
day hour sum(a.GemsPurchased) sum(b.Gems) sum(b.ShadowGems) GemRatio ShadowGemRatio
9/5/2014 0 472875 465499 60766 0.9844 0.1285
9/5/2014 1 350960 371092 45408 1.0574 0.1294
9/5/2014 2 472985 509618 58329 1.0775 0.1233
9/5/2014 3 1023905 629310 71017 0.6146 0.0694
9/5/2014 4 1273170 628697 74896 0.4938 0.0588
9/5/2014 5 998920 637709 64145 0.6384 0.0642
9/5/2014 6 876470 651451 68977 0.7433 0.0787
9/5/2014 7 669100 667217 81599 0.9972 0.122
What I'd like to do is create an 8th and 9th column which calculate the % change from previous row for both GemRatio and ShadowGemRatio. I've seen other threads here on how to do this for specific queries, but I couldn't get it to work for my particular MySQL query...
Ok first create a view for that query. Let's call it v1:
CREATE VIEW v1 AS SELECT YOUR QUERY HERE;
Now here is the query to have the ratios. I assumed a day has 24 hours. The first row ratio change will be zero.
select now.*,
CASE
WHEN yesterday.gemRatio is null THEN 0
ELSE 100*(now.gemRatio-yesterday.gemRatio)/yesterday.gemRatio
END as gemChange,
CASE
WHEN yesterday.ShadowGemRatio is null THEN 0
ELSE 100*(now.ShadowGemRatio-yesterday.ShadowGemRatio)/yesterday.ShadowGemRatio
END as shadowGemChange
from v1 now left outer join v1 yesterday on
((now.day = yesterday.day && now.hour = yesterday.hour+1) ||
(DATEDIFF(now.day,yesterday.day) = 1 && now.hour = 0 && yesterday.hour=23))
I'm having problems with timediff and DATE_SUB. Here is my MYSQL query:
SELECT id, clock_user_id, clock_date,
(Select clock_time from aura_clock where aura_clock.clock_type = 'Start' and
aura_clock.clock_date = t1.clock_date) as start, (Select clock_time
from aura_clock where aura_clock.clock_type = 'Stop'
and aura_clock.clock_date =
t1.clock_date) as Stop,
THE PROBLEM START HERE
TIMEDIFF((select clock_time FROM aura_clock t
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time
ORDER BY t.clock_time LIMIT 1), MIN(clock_time)) as spent
FROM aura_clock t1 WHERE t1.clock_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL
WEEKDAY(CURDATE()) DAY),INTERVAL 15 day)
AND t1.clock_date < DATE_SUB(curdate(),INTERVAL DAYOFWEEK(curdate()) + 6 day)
GROUP BY clock_date
The result is :
Now, I want to subtract 1 hour from the time spent using DATE_SUB but it didn't work.
as mirkobrankovic wrote:
((TIMEDIFF((select clock_time FROM aura_clock t
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time
ORDER BY t.clock_time LIMIT 1), MIN(clock_time))) - INTERVAL 1 HOUR) AS new_spent
should work
EDIT:
The best i got is time in seconds :
http://sqlfiddle.com/#!2/18160/66/0
a lot depends on MYSQL version