SQL select subquery runs forever when passing MONTH to sub query - mysql

The purpose of this query:
contracts in the database have a start date, contract date and closing date.
When a contract goes pending, the contract date is set and the closing date is set to a around 40 days in the future. I need to run a query that gets the contracts that have a contract date in the past and closing date that has not been reached to find the number of pending contracts for that month. This query generate a report of pending contracts from the last full month and going back 12 months.
My thought is to get the last day of each month and count the number of contracts that have closing date > the last day of month and contract date <= last day of month
The following query executes in 51ms. the query returns rows for July
SELECT DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 2 MONTH), '%Y-%m-%d 23:59:59') as lastDay,
count(*) as total FROM contracts
WHERE L_ClosingDate >= DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 2 MONTH), '%Y-%m-%d 23:59:59')
AND L_ContractDate <= DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 2 MONTH), '%Y-%m-%d 23:59:59')
Now I need to run the query to get rows grouped by month, so I altered the query to the following:
select MONTH(L_ClosingDate) as m, YEAR(L_ClosingDate) as y,
(SELECT count(*) FROM contracts WHERE L_ClosingDate >= DATE_FORMAT(LAST_DAY(CONCAT(y,'-',m,'-',LPAD(1,2,'00'))), '%Y-%m-%d 23:59:59')
AND L_ContractDate <= DATE_FORMAT(LAST_DAY(CONCAT(y,'-',m,'-',LPAD(1,2,'00'))), '%Y-%m-%d 23:59:59')
) as total
FROM contracts
WHERE L_ClosingDate > DATE_ADD(NOW(), INTERVAL -2 MONTH)
AND L_CLosingDate < DATE_ADD(NOW(), INTERVAL -1 MONTH)
GROUP BY YEAR(L_ClosingDate), MONTH(L_ClosingDate)
ORDER BY L_ClosingDate DESC
It executes forever...
I've tweaked it and found that the MONTH and YEAR 'm' and 'y' in the subquery is causing the problem. If I hardcode a date it executes as expected.
Expected output:
Month | Year | total
8 | 2015 | 74
7 | 2015 | 87
6 | 2015 | 45
I'm working on getting some sample data
Is there another way to perform the group by query?

How about this? (Assumes closing date is a datetime)
SELECT MONTH(L_ClosingDate) as m, YEAR(L_ClosingDate) as y
, count(*) as total
FROM contracts
WHERE L_ClosingDate >= LAST_DAY(CURDATE() - INTERVAL 3 MONTH) + 1 DAY
AND L_CLosingDate < LAST_DAY(CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY
GROUP BY m, y
ORDER BY y DESC, m DESC
;

The easy way of solve this is create a months table, and that is easy to do because only take 1200 rows for whole century.
CREATE TABLE months (
month_id int,
beginDay date,
lastDay date
)
Then your query become much more simple. Just join and calculate between

Related

I want to calculate time between 2 Dates of a day with multiple rows in mysql

I'm using this query to calculate the login time of a user on the app for the whole day and previous 5 days
Select
sec_to_time(sum(time_to_sec(TIMEDIFF((IFNULL(logoff_time, ADDTIME(now(), '05:00:00'))),login_time)))) as online_time
from tb_sessions
WHERE
(login_time BETWEEN DATE(DATE_ADD(now(), INTERVAL (-6) DAY))
AND
ADDTIME(now(), '5:00:00')) AND user_id = 30982
AND TIME(`login_time`) between "00:00:00" AND "23:59:59"
group by DATE(login_time)
Now i have some new requirements:
Calculate time from 07:00:00 to 23:59:59
My Table: tb_sessions
id | user_id | login_time | logoff_time
1 3098 2017-06-10 06:30:00 2017-06-10 07:45:00
2 3098 2017-06-10 07:45:01 2017-06-10 08:30:00
By using above query total oline time is = 02:00:00
But i want only time from 7:00 to 8:30, so total time will be = 1:30:00
I make some changes in query with cases but no success.
You can check my query on the below link:
http://sqlfiddle.com/#!9/4620af/12
You could use greatest to take the latest of the dates login_time and 7:00 on the same day, and then use greatest again to exclude negative time differences (when also logoff time is before 7:00):
Select date(login_time) date,
time_format(sec_to_time(sum(greatest(0, time_to_sec(timediff(
ifnull(logoff_time, now()),
greatest(login_time, date_add(date(login_time), interval 7 hour))
))))), '%H:%i:%s') online
from tb_sessions
where login_time between date(date_add(now(), interval (-3) day)) and now()
and user_id = 3098
and time(login_time) between "00:00:00" and "23:59:59"
group by date(login_time)
See it run on sqlfiddle
Explanation
The inner greatest call looks like this:
greatest(login_time, date_add(date(login_time), interval 7 hour))
The second argument takes the date-only from the login_time, so it corresponds to midnight of that day, and then adds 7 hours to it: so this represents 7:00 on that day. greatest will return the latest of these two timestamps. If the first argument represents a time than 7:00, it will be returned. If not, the second argument (i.e. 7:00) will be returned.
The outer greatest call looks like this:
greatest(0, time_to_sec(timediff(....)))
This will make sure the time difference is not negative. Take this example record:
login_time | logoff_time
----------------+----------------
2017-06-01 6:30 | 2017-06-01 6:45
In this case the innermost greatest will return 2017-06-01 7:00, because 6:30 is too early. But that will make timediff() return a negative time interval: -15 minutes. What we really want is 0, because there is no time the user was logged on after 7:00. This is what greatest will do: greatest(0, -15) = 0, so the negative value will be eliminated and will not influence the sum.
Condition on login_time
I left the condition time(login_time) between "00:00:00" and "23:59:59" there, but it really does not do anything, since that is true for all times (unless they are null, but then they would not pass the first condition either).
Edit after New Requirements
In comments you asked how to group by each day when a user doesn't log off on the same day but stays online until 1 or 2 days later.
In that case you need a helper table that will list all days you want to see in the output. This could for instance be seven records for the 7 last days.
Then you have to join your table with it so that there is at least an overlap of the user's session with such a reference date. The calculation of the online time will have to take into account that the log off time might not be before mid night.
Here is the updated query:
select ref_date date,
time_format(sec_to_time(sum(greatest(0, time_to_sec(timediff(
least(ifnull(logoff_time, now()), date_add(ref_date, interval 1 day ), now()),
greatest(login_time, date_add(ref_date, interval 7 hour))
))))), '%H:%i:%s') online
from ( select date(date_add(now(), interval (-6) DAY)) as ref_date union all
select date(date_add(now(), interval (-5) DAY)) union all
select date(date_add(now(), interval (-4) DAY)) union all
select date(date_add(now(), interval (-3) DAY)) union all
select date(date_add(now(), interval (-2) DAY)) union all
select date(date_add(now(), interval (-1) DAY)) union all
select date(now())
) ref
inner join tb_sessions
on login_time < date_add(ref_date, interval 1 day)
and logoff_time > date_add(ref_date, interval 7 hour)
where user_id = 3098
group by ref_date
See it run on sqlfiddle.

add days to a date column and then check if it's between two dates SQL

I need a sql query to find the data from a employee table.
Startdate is present in employee table.
I need to add 90 days in startdate and then I need to check if the startdate lies in the current month or not.
I did try below query:
SELECT *
FROM `employees`
WHERE DATE_ADD(str_to_date(startdate, '%m/%d/%Y'),INTERVAL 90 DAY) BETWEEN '09/01/2016' AND '09/30/2016'
but its not giving me the expected results.(I do have data which should show up if the query is correct.)
Hi i did change the query and ran, please see the result. I am getting results of next month too :(
This is the query
SELECT id,startdate from employees WHERE str_to_date(startdate, '%m/%d/%Y') between DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day)
Query output
Rephrased my query...
Where the event was within the dates 90 days before the start and end of this month
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day) -- End of this month - 90 days
or, for three months
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 3 month) -- start of this month - 3 months
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 3 month) -- End of this month - 3 months

How to get average sales of an employee before certain sale in MySQL?

Suppose I have a table with 3 columns: EMPLOYEE_ID, NUM_SALES, DATE. Simply this is the table of Employees indicating daily sales. For each row in the table, I try to compute this; average number of sales of that EMPLOYEE_ID in the last K days excluding this day.
How can I query this in MySQL? I try to group by with EMPLOYEE_ID and DATE but I cannot figure out how to find last K sales for each row.
To select an interval of days, you can use MySQL's DATE_SUB() function:
WHERE `date` >= DATE_SUB(NOW(), INTERVAL 3 DAY)
This will select all records that are from the past 3 days. However, to exclude "today" from that:
WHERE `date` BETWEEN
DATE_SUB(NOW(), INTERVAL 3 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
After that you should be able to GROUP BY the employee_id to get what you're after:
SELECT
employee_id, avg(num_sales) AS avg_num_sales
FROM
employee_table
WHERE `date` BETWEEN
DATE_SUB(NOW(), INTERVAL 3 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY
employee_id
You need to be able to select items from your table, let's call it dailysale, by date.
Here's what you do.
SELECT employee_id, AVG(num_sales) AS avg_sales
FROM dailysale
WHERE date >= CURDATE() - INTERVAL 3 DAY
AND date < CURDATE()
GROUP BY employee_id
This uses two WHERE clauses to winnow down the date range you're using. date >= CURDATE() - INTERVAL 3 DAY excludes all records before midnight three days ago, and date < CURDATE() excludes all records on or after midnight today.
You need to use CURDATE() rather than NOW() because, well, NOW() includes the date and the present time of day. date < NOW() will include today's sales, because your date column only records dates and not times.
If you want to list the employees in order of sales, you could add
ORDER BY AVG(num_sales) DESC, employee_id
to the query.

MySQL: search orders made since yesterday 3pm till today 4pm

I have table ORDERS where is stored data about orders with their status and the date of order. I would like to search all orders with specified status and which was made yesterday after 3pm untill today 4pm. The query will run in different times (10am, 3pm, 5 pm... regardless).
So on example: if I run the query today (13.05.2014) I would like to get all orders made from 2014-12-05 15:00:00 untill 13-05-2015 16:00:00
The date is stored in format: YYYY-MM-DD HH:MM:SS
What I got is:
select *
from orders
where status = 'new'
and (
(
date_add(created_at, INTERVAL 1 day) = CURRENT_DATE()
and hour(created_at) >= 15
) /*1*/
or (
date(created_at) = CURRENT_DATE()
and hour(created_at) <= 16
) /*2*/
)
And I get only orders made today - like only the 2nd condition was taken into account.
I prefer not to use created >= '2014-05-12 16:00:00' (I will not use this query, someone else will).
When you add an interval of 1 day to the date/time, you still keep the time component. Use date() for the first condition:
where status = 'new' and
((date(date_add(created_at, INTERVAL 1 day)) = CURRENT_DATE() and
hour(created_at) >= 15
) /*1*/ or
(date(created_at) = CURRENT_DATE() and
hour(created_at) <= 16
) /*2*/
)
And alternative method is:
where status = 'new' and
(created_at >= date_add(CURRENT_DATE(), interval 15-24 hour) and
created_at <= date_add(CURRENT_DATE(), interval 16 hour)
)
The advantage of this approach is that all functions are moved to CURRENT_DATE(). This would allow MYSQL to take advantage of an index on created_at.

group by day for the past 5 days

I am trying to select the sum of an integer field for the past 5 days, and I need to group it for each day.
I'm having a bit of issues figuring out the grouping. Here's my sql query so far:
select
sum(`amount_sale`) as total
from `sales`
where the_date >= unix_timestamp((CURDATE() - INTERVAL 5 DAY))
that works fine for generating the sum for all 5 days together, but I need to break this down so that it shows the sum for each of the past 5 days i.e:
day 1 - $200
day 2- $500
day 3 - $20
etc.
SELECT DATE(FROM_UNIXTIME(the_date)) AS dt, SUM(amount_sale) AS total
FROM sales
WHERE the_date >= UNIX_TIMESTAMP((CURDATE() - INTERVAL 5 DAY))
GROUP BY
dt
To returns 0 for missing dates:
SELECT dt, COALESCE(SUM(amount_sale), 0) AS total
FROM (
SELECT CURDATE() - INTERVAL 1 DAY AS dt
UNION ALL
SELECT CURDATE() - INTERVAL 2 DAY AS dt
UNION ALL
SELECT CURDATE() - INTERVAL 3 DAY AS dt
UNION ALL
SELECT CURDATE() - INTERVAL 4 DAY AS dt
UNION ALL
SELECT CURDATE() - INTERVAL 5 DAY AS dt
) d
LEFT JOIN
sales
ON the_date >= UNIX_TIMESTAMP(dt)
AND the_date < UNIX_TIMESTAMP(dt + INTERVAL 1 DAY)
GROUP BY
dt
This is not a very elegant solution, however, MySQL lacks a way to generate recordsets from scratch.
use the format function to return weekday nr: SELECT DATE_FORMAT(the_date, '%w');
use between
like select * from XXX where date between date(...) and date(...) group by date Limit 0,5
should do it