I am working on a report module. I have worked on comparing this week and last weeks report for certain agents.
This weeks Report query is as follows
SELECT COUNT(created_at) AS cust_count_new, agency_id, created_at FROM customers WHERE
(customers.created_at >= DATE(NOW()) - INTERVAL 6 DAY AND customers.created_at
< DATE(NOW()) + INTERVAL 1 DAY
Last Week Report query is as follows
SELECT COUNT(created_at) AS cust_count_old, agency_id, created_at FROM customers WHERE
(customers.created_at >= DATE(NOW()) - INTERVAL 13 DAY AND customers.created_at
< DATE(NOW()) - INTERVAL 6 DAY
What I am doing is comparing this weeks and last weeks report. Now how can i change the query to this month and last months (30 days). I am little confused, so any help is appreciated.
You can use MONTH as unit in the INTERVAL. Also, you can use CURDATE() instead of DATE(NOW()), to get the current date.
This Month Report query will be:
SELECT COUNT(created_at) AS cust_count_new,
agency_id,
created_at
FROM customers
WHERE customers.created_at >= CURDATE() - INTERVAL 1 MONTH AND
customers.created_at < CURDATE() + INTERVAL 1 DAY
Last Month Report query is as follows
SELECT COUNT(created_at) AS cust_count_new,
agency_id,
created_at
FROM customers
WHERE customers.created_at >= CURDATE() - INTERVAL 2 MONTH AND
customers.created_at < (CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY
Related
I have a Table in Database which has records of Logins.
Table name: user_logins
ID | timestamp
1 2019.01.03 (Year, Month, Day)
2 2019.01.04
3 2019.01.05
4 2019.01.05
5 2019.01.07
6 2019.01.07
7 2019.01.09
I want to Show only Count of Records by this Week.
From Monday to Sunday (04-02-2019 ... 10-02-2019)
My PHP and SQL Code is:
$mo = mysql_num_rows(mysql_query('SELECT * FROM user_logins WHERE DAYNAME(DATE(timestamp)) = "monday" and timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFWEEK(CURDATE())-0 DAY)'));
this should show the records of 04-02-2019
Here is my SQL Fiddle link:
SQL Fiddle
This:
DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
gives this week's Monday.
So:
SELECT * FROM user_logins
WHERE
timestamp
BETWEEN DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
and
NOW()
Try following query:
SELECT id FROM `user_logins`
WHERE timestamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND timestamp < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Demo
In my DB I have a column called dato_tid (Datatype = date)
right now I have 2 post
1 where the date is 2018-07-18
2 where the date is 2018-07-20
I need to select the post, that has less then 24 hours to go
SELECT * FROM `udflyt` WHERE dato_tid > DATE_ADD(CURDATE(), INTERVAL -1 day)
this line will select both posts
SELECT * FROM `udflyt` WHERE dato_tid > DATE_ADD(CURDATE(), INTERVAL -24 HOUR)
and so will this, I did try to change the > to < but the same.
SELECT * FROM `udflyt` WHERE dato_tid > (now() - interval 1 day )
this line will also get both posts
So what do I need to do, Thanks
Actually, 24 hours to go means you should ADD a day, not subtract a day.
SELECT *
FROM `udflyt`
WHERE dato_tid <= DATE_ADD(CURDATE(), INTERVAL +1 day);
Will provide only the record with date '2018-07-18' (which is what you are looking for, I believe.
The below shows the values used for comparison for both doing addition and subtraction.
SELECT *, DATE_ADD(CURDATE(), INTERVAL +1 day), DATE_ADD(CURDATE(), INTERVAL -1 day)
FROM `udflyt`
WHERE dato_tid <= DATE_ADD(CURDATE(), INTERVAL +1 day);
Hi I have a MySQL database that already has auto increment date time colomn (may information help).
How to filter last one week data as start from specific day until next 7 day.
example. Show data from Wednesday on this week until nest 7 day
$query="SELECT * FROM diagnosa WHERE schedule= '$a'";
$a is start day the data start to be shown (example Wednesday)
This may help you...
SELECT *
FROM table
WHERE date_colunm between 'start_date' AND DATE_ADD("start_date", INTERVAL 7 DAY);
UPDATE :
As you need from the Wednesday. Please us below query
SELECT *
FROM table
WHERE date_colunm
between (
CASE
WHEN DAYOFWEEK(curdate()) > 4
THEN DATE_SUB(NOW(), INTERVAL (7-DAYOFWEEK(curdate())) DAY)
ELSE (
CASE
WHEN DAYOFWEEK(curdate()) < 4
THEN DATE_ADD(NOW(), INTERVAL (4-DAYOFWEEK(curdate())) DAY)
ELSE curdate()
END
)
END
) as date
AND DATE_ADD (
(
CASE
WHEN DAYOFWEEK(curdate()) > 4
THEN DATE_SUB(NOW(), INTERVAL (7-DAYOFWEEK(curdate())) DAY)
ELSE (
CASE
WHEN DAYOFWEEK(curdate()) < 4
THEN DATE_ADD(NOW(), INTERVAL (4-DAYOFWEEK(curdate())) DAY)
ELSE curdate()
END
)
END
) as date, INTERVAL 6 DAY
)
NOTE :
There are some codes for the days like for Wednesday we have 4 (as mentioned in query),
so you can change the code according to your requirements. Like for Monday is : 2
I hope following query will give you the idea what I am looking for-
SELECT SUM(t1.hours) AS totalhours FROM
(
SELECT (time_to_sec(timediff(time_out, time_in)) / 3600) AS hours FROM bb_work_log
WHERE user_id = 6 AND (working_date BETWEEN '2014-04-01' AND '2014-04-31')
) AS t1
In my query, you can see the working_date which I given here manually. But, I would not like to do it manually. I would like to pick first day and last day of current month dynamically.
First day of Previous Month
select last_day(curdate() - interval 2 month) + interval 1 day
Last day of Previous Month
select last_day(curdate() - interval 1 month)
First day of Current Month
select last_day(curdate() - interval 1 month) + interval 1 day
Last day of Current Month
select last_day(curdate())
You can use LAST_DAY(NOW() - INTERVAL 1 MONTH) + INTERVAL 1 DAY,which will subtract one month from now and by by adding 1 day in LAST_DAY of previous month will give you the first day of current month
SELECT SUM(t1.hours) AS totalhours FROM
(
SELECT (time_to_sec(timediff(time_out, time_in)) / 3600) AS hours FROM bb_work_log
WHERE user_id = 6
AND (working_date BETWEEN LAST_DAY(NOW() - INTERVAL 1 MONTH)
AND LAST_DAY(NOW()))
) AS t1
LAST_DAY(NOW() - INTERVAL 1 MONTH) this will give you the last day of
previous month
First/Last day of Month Fiddle Demo
You can achieve it these ways ----
/* Current month*/
SELECT DATE_SUB(LAST_DAY(NOW()),INTERVAL DAY(LAST_DAY(NOW()))-1 DAY),CONCAT(LAST_DAY(NOW()),' 23:59:59');
SELECT LAST_DAY(CURDATE()) - INTERVAL DAY(LAST_DAY(CURDATE()))-1 DAY ,CONCAT(LAST_DAY(NOW()),' 23:59:59');
/* previous month*/
SELECT DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH,'%Y-%m-01 00:00:00'),DATE_FORMAT(LAST_DAY(CURDATE()-INTERVAL 1 MONTH),'%Y-%m-%d 23:59:59');
-- first day of previous month
set #start_date = date_format(NOW() - INTERVAL 1 MONTH, '%Y-%m-01');
-- last day of previous month
set #end_date = date_format(NOW() , '%Y-%m-01') - INTERVAL 1 day;
select #start_date ,#end_date ;
-- first day of current month
set #start_date = date_format(NOW(), '%Y-%m-01');
-- last dat of current month
set #end_date = date_format(NOW() + INTERVAL 1 MONTH, '%Y-%m-01') - INTERVAL 1 day;
select #start_date ,#end_date ;
I have a table with 3 columns: id, updated_at, click_sum.
Many rows have the exact same updated_at value which makes it hard to simply retrieve the data, order by updated_at and display the sums in a chart.
Since there are multiple sums for the same dates which screws the chart.
What I try to achieve is to get the following output:
update_at | click_sum
-----------+-----------
date1 | 100
date2 | 3
date3 | 235
date4 | 231
Optionally only those dates which are form the last month, week or day AND not simply the dates which are NOW() - 1 month.
The current query I build is very large and doesn't work that well.
It groups by dates (no duplicated dates appear) and SUM()s the clicks correctly but defining from when (last month, week, day) the dates are doesn't seem to work properly.
Query: ($interval stands for MONTH or DAY or SECOND or WEEK)
SELECT d.updated_at, SUM(d.clicks_sum) AS click_sum
FROM aggregated_clicks d
JOIN
(
SELECT c.id, MAX(StartOfChains.updated_at) AS ChainStartTime
FROM aggregated_clicks c
JOIN
(
SELECT DISTINCT a.updated_at
FROM aggregated_clicks a
LEFT JOIN aggregated_clicks b ON (b.updated_at >= a.updated_at - INTERVAL 1 DAY AND b.updated_at < a.updated_at)
WHERE b.updated_at IS NULL
) StartOfChains ON c.updated_at >= StartOfChains.updated_at
GROUP BY c.id
) GroupingQuery
ON d.id = GroupingQuery.id
WHERE GroupingQuery.ChainStartTime >= DATE_SUB(NOW(), INTERVAL 1 $interval)
GROUP BY GroupingQuery.ChainStartTime
ORDER BY GroupingQuery.ChainStartTime ASC
maybe I'm assuming too much about the nature of your question (and the table it refers to), but I think this can be done much more simply than the query you've shown.
figuring the latest completed month isn't very hard.
it starts with knowing the first date of this current month -- use this:
date_sub(curdate(), interval (extract(day from curdate())-1) day)
and to know the first day of that previous month, use this:
date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
so if you want to get the sums for just the days in between -- i.e. the latest completed month, use this:
select updated_at, sum(click_sum) from aggregated_clicks
where updated_at >= date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
and updated_at < date_sub(curdate(), interval (extract(day from curdate())-1) day)
group by updated_at;
figuring the lastest completed week is just as easy. this example will assume a Sunday-Saturday week.
because of the way the ODBC standard defines date numbers, it's easy to find the end (Saturday) of the previous week:
date_sub(curdate(), interval dayofweek(curdate()) day)
and the beginning (Sunday) of that week is six days before that:
date_sub(curdate(), interval (dayofweek(curdate())+6) day)
so if you want to get the sums for just the days in between -- i.e. the latest completed week, use this:
select updated_at, sum(click_sum) from aggregated_clicks
where updated_at >= date_sub(curdate(), interval (dayofweek(curdate())+6) day)
and updated_at <= date_sub(curdate(), interval dayofweek(curdate()) day)
group by updated_at;
and of course figuring based on the latest completed day is super easy.
to get the date of the previous day, use this:
date_sub(curdate(), interval 1 day)
so if you want the sums just for yesterday, use this:
select updated_at, sum(click_sum) from aggregated_clicks
where updated_at = date_sub(curdate(), interval 1 day)
group by updated_at;
NOTE: I've tested these queries using MySQL 5.1, YMMV.
----------
UPDATE: since the date column is a datetime, simply change all references to updated_at in my queries to date(updated_at) like so:
month case:
select date(updated_at), sum(click_sum) from aggregated_clicks
where date(updated_at) >= date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
and date(updated_at) < date_sub(curdate(), interval (extract(day from curdate())-1) day)
group by date(updated_at);
week case:
select date(updated_at), sum(click_sum) from aggregated_clicks
where date(updated_at) >= date_sub(curdate(), interval (dayofweek(curdate())+6) day)
and date(updated_at) <= date_sub(curdate(), interval dayofweek(curdate()) day)
group by date(updated_at);
yesterday case:
select date(updated_at), sum(click_sum) from aggregated_clicks
where date(updated_at) = date_sub(curdate(), interval 1 day)
group by date(updated_at);