What difference between - mysql

I have first query
select count(*)
from `order`
where marketer_id = 75 and
HandleStatus != -1 and
(Created_at BETWEEN '2017-05-01' AND '2017-05-31')
and result is 1050
i also have second query :
select count(*)
from `order`
where marketer_id = 75 and
HandleStatus != -1 and
(Month(Created_at) =5 and Year(Created_at) = 2017)
and result is 1111
I think 2 query have same meaning but it return 2 different result.
Info about column "Created_at": COLUMN_NAME Created_at, COLUMN_TYPE timestamp, IS_NULLABLE NO, COLUMN_KEY , COLUMN_DEFAULT CURRENT_TIMESTAMP
Please help what difference between 2 query?

If you consider the time within a day, the first query only returns results before 2017-05-31 00:00:00.
If you have any results after 2017-05-31 00:00:00 and before 2017-05-31 23:59:59 (maybe down to milliseconds too), they only show up in the second query.

The first query is not looking at 31st May, it looks only until 30 May. The absence of a time component means the time is taken as midnight, or the start of the 31st.

Between does take only up to '2017-05-30 23:59:59' and after 2017-05-01 00:00:00. It should not consider 31th full day.
If you want first query return same data as second query you can use like this
select count(*)
from `order`
where marketer_id = 75 and
HandleStatus != -1 and
(Created_at >= '2017-05-01' AND Created_at < '2017-06-01')

Related

In mysql how to get users that have no event X in past 14 days and neither in the future

Consider I have the following table and current date is 2022-09-01:
Requirement: I want to get all users that have no event_name like cbt care in the past 14 days and onwards into the future.
I have this query:
SELECT * FROM test_table
WHERE event_name LIKE "%cbt care%"
AND start_date <= DATE_SUB(NOW(), INTERVAL 14 DAY)
;
Which returns:
The issue is that user_id = x does have a cbt care event in 2022-09-10 which is 9 days ahead of current date (2022-09-01).
How to return only users satisfy requirement posted above?
SELECT user_id,
COUNT(CASE WHEN event_name LIKE '%cbt care%' AND start_date
> CURDATE() - INTERVAL 14 day THEN 1 END) AS count_recent
FROM test_table
GROUP BY user_id
HAVING count_recent = 0;
https://www.db-fiddle.com/f/64j7L1VZsVdLYqmcQ2NrvV/0
The CASE expression returns 1 for each row with the conditions you described (a specific event name and a start date after the date 14 days ago, which includes all of the future dates too). For rows that don't match that condition, the CASE returns NULL. There's an implicit ELSE NULL in any CASE expression.
COUNT(<expr>), like many set functions, ignores NULLs. It will only count the occurrences of non-NULL values. So if the count returns 0, then the CASE returned only NULLs, which means there are no recent or future 'cbt care' events for that user.
select id
,user_id
,event_name
,start_date
from (
select *
,count(case when abs(datediff(curdate(),start_date)) <= 14 and event_name like "%cbt care%" then 1 end) over (partition by user_id) as cw
from t
) t
where cw = 0
id
user_id
event_name
start_date
0
a
cbt care
2022-06-01 20:00:00
Fiddle

Find number of rows for each hour where datetime columns match certain criteria

RDBMS: MySQL
The time column(s) datatype is of datetime
For every hour of the 24 hour day I need to retrieve the number of rows in which their start_time matches the hour OR the end_time is great than or equal to the hour.
Below is the current query I have which returns the data I need but only based off of one hour. I can loop through and do 24 separate queries for each hour of the day but I would love to have this in one query.
SELECT COUNT(*) as total_online
FROM broadcasts
WHERE DATE(start_time) = '2018-01-01' AND (HOUR(start_time) = '0' OR
HOUR(end_time) >= '0')
Is there a better way of querying the data I need? Perhaps by using group by somehow? Thank you.
Not exactly sure if i am following, but try something like this:
select datepart(hh, getdate()) , count(*)
from broadcasts
where datepart(hh, starttime) <=datepart(hh, endtime)
and cast(starttime as date)=cast(getdate() as date) and cast(endtime as date)=cast(getdate() as date)
group by datepart(hh, getdate())
Join with a subquery that returns all the hour numbers:
SELECT h.hour_num, COUNT(*) AS total_online
FROM (SELECT 0 AS hour_num UNION SELECT 1 UNION SELECT 2 ... UNION SELECT 23) AS h
JOIN broadcasts AS b ON HOUR(b.start_time) = h.hour_num OR HOUR(b.end_time) >= h.hour_num
WHERE DATE(b.start_time) = '2018-01-01'
GROUP BY h.hour_num

How to count the numbers of rows within a certain days

I would like to perform a query that counts the number of rows that have a close date within 60 days of open_date by store.
Sample: Table A
Store Open Date Close Date
A 2017-01-01 2017-01-31
B 2017-02-02 Null
A 2017-01-02 2018-01-21
Thanks in advance.
Use datediff()
SELECT count(*) FROM [Table A] WHERE datediff(Close_date,Open_date) <= 60;
Try this on of the queries below:
SELECT count(*) numberOfDays
FROM TableA
WHERE close_date>=CURRENT_DATE - INTERVAL 60 DAY;
SELECT count(*) numberOfDays
FROM TableA
WHERE close_date>=CURRENT_DATE - INTERVAL '60' DAY;
See it work on SQL Fiddle.
select store, open_date, count(*)
from tableA
where close_date between date_sub(open_date, interval 60 DAY) and date_add(open_date, interval 60 DAY)
group by store, open_date;
This will work but your starting date has to either be current date or the open_date (which is what your question was). Given that the open_date appears to be different for each row, your count is going to be ... interesting.
Also, count will always return something, so unless you use an if or case statement keying on 0 (zero) it will return a 0 not a null.

how to sort Date in mysql

scheduled_datetime
2016-12-07 10:30:00
2016-12-07 13:30:00
2016-11-06 22:30:00
2016-12-06 23:30:00
2016-19-08 22:30:00
sorting first date wise if date is same then sort by time
select *
from mytable
where scheduled_datetime > now()
order by scheduled_datetime;
from this query i have to find date which greater than today time please suggest me where am doing wrong .
If you want to only output dates which are greater than today, then you need to use DATE() to only compare to the date values of scheduled_datetime:
select *
from mytable
where DATE(scheduled_datetime) > NOW()
order by scheduled_datetime asc;
You have following 3 options :
--If you want to fetch records greater than current date & time
select * from mytable where scheduled_datetime > NOW()
--If you want to fetch records greater than current date
select * from mytable where scheduled_datetime > CURDATE()
--If you want to fetch records greater than current time
select * from mytable where scheduled_datetime > CURTIME()
You can use ORDER BY ASC or DESC according, in what order you want to get data.

MySQL TIMEDIFF() returns the differences between two times and Sum of time

my query is i want find the sum with the timediff function any help
SELECT TIMEDIFF( timefrom, timeto )
FROM `leaveapply`
WHERE userid = '21'
GROUP BY leavetype
LIMIT 0 , 30
The following statement will return the time like this
00:00:00
00:00:00
01:00:00
02:00:00
i want to sum and get the result ,
like this 03:00:00
Try something like this:
select sum(
SELECT TIMEDIFF( timefrom, timeto ) FROM `leaveapply`
WHERE userid = '21'
GROUP BY leavetype
) as total