I do know that the following conditions will return the total number in the past 7 days
SELECT count(id) FROM registration
WHERE createdDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
Is there a single query that I can do to get the past 7 days returning in an array of 7 results of each individual day?
For example:
day 1 - 10
day 2 - 5
day 3 - 9
..
..
..
This will give you the date and the count.
SELECT DATE(createdDate),COUNT(id)
FROM registration
WHERE createdDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY DATE(createdDate)
Alternatively to give a result closer to your example you could use:
SELECT CONCAT("Day ",DATEDIFF(NOW(), createdDate)) AS day,COUNT(id)
FROM registration
WHERE createdDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY DATE(createdDate)
Add a group-by clause:
SELECT count(id), DATE(createdDate)
FROM registration
WHERE createdDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY DATE(createdDate)
select 8 - n day, count(id)
from registration
join (select 1 n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7) x
on createdDate between date_sub(curdate(), interval n day) and date_sub(curdate(), interval n-1 day)
group by day
order by day
Related
I want to get the today count of users and yesterday's users count for that i want to write only one query how can i do that..?
these are my queries I want only one query:
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,1 as todayCount
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,0 as yesterdayCount
My expected results or only 2 columns
todayCount yesterdayCount
2 4
This should do the trick:
SELECT COUNT(CASE
WHEN visited_date = CURDATE() THEN 1
END) AS todayCount ,
COUNT(CASE
WHEN visited_date = CURDATE() - INTERVAL 1 DAY THEN 1
END) AS yesterdayCount
FROM visitors
WHERE visited_date IN (CURDATE(), CURDATE() - INTERVAL 1 DAY)
GROUP BY visited_date
ORDER by visited_date
If you know the current and previous date, then you can do:
SELECT SUM(visited_date = CURDATE()) as today,
SUM(visited_date = CURDATE() - interval 1 day) as yesterday
FROM visitors
WHERE visited_date >= CURDATE() - interval 1 day;
If you don't know the two days, then you can do something similar, getting the latest date in the data:
SELECT SUM(v.visited_date = m.max_vd) as today,
SUM(v.visited_date < m.max_vd) as yesterday
FROM visitors v CROSS JOIN
(SELECT MAX(v2.visited_date) as max_vd FROM visitors v2) as m
WHERE v.visited_date >= m.max_vd - interval 1 day
Just try this simple query
select visited_date as date, COUNT(*) as count from `visitors`
group by `visited_date` order by `visited_date` asc
It will produce output as
It will work for you.
Try this:
$sqlToday = "Select COUNT(*) FROM menjava WHERE DATE(date_submitted)=CURRENT_DATE()";
$sqlYesterday = "Select COUNT(*) FROM menjava WHERE DATE(dc_created) = CURDATE() - INTERVAL 1 DAY";
I need to perform two different counts in one single query.
First Query: count number of transactions from today 30 days back.
Second Query: count number of transactions from last 60 until last 30 days.
I have first query working fine as:
SELECT
COUNT(*) AS sales
FROM
transactions
WHERE DATE(created) > DATE_SUB(NOW(), INTERVAL 30 DAY)
AND STATUS = 1;
How can I incorporate the second query into the above?
You can use COUNT and CASE WHEN:
SELECT
COUNT(CASE WHEN DATE(created) > DATE_SUB(NOW(), INTERVAL 30 DAY) THEN 1 END) AS c1,
COUNT(CASE WHEN DATE(created) <= DATE_SUB(NOW(), INTERVAL 30 DAY) THEN 1 END) AS c2
FROM transactions
WHERE DATE(created) > DATE_SUB(NOW(), INTERVAL 60 DAY)
AND STATUS = 1;
or UNION:
SELECT COUNT(*) AS sales
FROM transactions
WHERE DATE(created) > DATE_SUB(NOW(), INTERVAL 30 DAY)
AND STATUS = 1
UNION ALL
SELECT COUNT(*)
FROM transactions
WHERE DATE(created) > DATE_SUB(NOW(), INTERVAL 60 DAY)
AND DATE(created) < DATE_SUB(NOW(), INTERVAL 30 DAY)
AND STATUS = 1
I have entries for each days as a punch in/out clock. I would like to display all days for the date givens week and show the days even if no data in found for that day.
Example:
Monday 2015-05-04
- 2015-05-04 10:30:00
- 2015-05-04 15:45:34
Tuesday 2015-05-05
- 2015-05-05 08:43:23
- 2015-05-05 17:18:13
Wednesday 2015-05-06
- 2015-05-06 09:03:12
Thursday 2015-05-07
0 Entries
Friday 2015-05-08
0 Entries
The DB schema looks like:
id | user_id | punch_time | punch_status
I would ideally like to be able to change the date given to MySQL to any timestamp and it would show the days and results for that week.
Thanks :D
NEW
Any ideas why this doesn't work to get days when no records are present?
SELECT * FROM punch_clock, calendar_table WHERE calendar_table.dt = DATE(punch_clock.punch_time) && calendar_table.week_num = $week && calendar_table.y = $year ORDER BY punch_clock.punch_time
Newer Query
SELECT * FROM punch_clock LEFT JOIN calendar_table ON calendar_table.dt = DATE(punch_clock.punch_time) WHERE calendar_table.week_num = 18 && calendar_table.y = 2015;
With MySQL, I usually use a calendar table for this purpose (containing all the dates until 2030 for example)
It allows to do many other things like this kind of queries, manage special days, etc..
You'll want to LEFT JOIN your table on it, I mean this calendar table have to be "left positioned"
Taking your last query, I'd do this:
SELECT *
FROM calendar AS cal
LEFT JOIN punch_clock AS puc
ON (cal.dt = DATE(puc.punch_time))
WHERE TRUE
AND cal.week_num = 18
AND cal.y = 2015
;
Didn't try, but this is the idea.
Have a try of this:
SELECT *
FROM
(
SELECT a.Date AS mydate
FROM (
SELECT date('2015-05-08') - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Date
FROM (SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9) AS c
) a
WHERE a.Date BETWEEN '2015-05-04' AND '2015-05-08'
) dates
LEFT JOIN
(
SELECT *
FROM
table1
) data
ON DATE_FORMAT(dates.mydate, '%Y%m%d') = DATE_FORMAT(data.punch_time, '%Y%m%d')
SQL Fiddle: http://sqlfiddle.com/#!9/72ee3/15/0
This is a fast but not that ideal solution for your question. But I think it's just enough for use.
If you'd like to solve the problem "perfectly", I suggest you reading this article: http://www.brianshowalter.com/calendar_tables
Even while this is a really old question I did not like the idea of an extra calendar table and came up with this query, adjusted for the question.
SELECT pc.`id`, pc.`user_id`, pc.`punch_status`, dates.`date` FROM punch_clock pc RIGHT JOIN (SELECT week.`date` FROM (
SELECT CURDATE() AS `date`
UNION SELECT CURDATE() + INTERVAL 1 DAY
UNION SELECT CURDATE() + INTERVAL 2 DAY
UNION SELECT CURDATE() + INTERVAL 3 DAY
UNION SELECT CURDATE() + INTERVAL 4 DAY
UNION SELECT CURDATE() + INTERVAL 5 DAY
UNION SELECT CURDATE() + INTERVAL 6 DAY
UNION SELECT CURDATE() - INTERVAL 1 DAY
UNION SELECT CURDATE() - INTERVAL 2 DAY
UNION SELECT CURDATE() - INTERVAL 3 DAY
UNION SELECT CURDATE() - INTERVAL 4 DAY
UNION SELECT CURDATE() - INTERVAL 5 DAY
UNION SELECT CURDATE() - INTERVAL 6 DAY
) AS week
WHERE YEARWEEK(`date`, 0) = YEARWEEK(CURDATE(), 0)) AS dates ON dates.`date` = DATE(pc.`punch_time`) ORDER BY dates.`date`
Not exactly pretty, but it does the job without an extra table. This uses a week from Monday to Sunday, use YEARWEEK(xxx, 1) for Sunday to Monday week.
Is there a way to group rows by a 7 days intervals(datetime) starting from a certain date in Mysql?
SELECT
1 + DATEDIFF(columnDate, #start_date) DIV 7 AS weekNumber
, #start_date + INTERVAL (DATEDIFF(columnDate, #start_date) DIV 7) WEEK
AS week_start_date
, MIN(columnDate) AS actual_first_date
, MAX(columnDate) AS actual_last_date
, SUM(otherColumn)
, AVG(otherColumn)
---
FROM
tableX
WHERE
columnDate >= #start_date
GROUP BY
DATEDIFF(columnDate, #start_date) DIV 7 ;
SELECT *
FROM `table`
GROUP BY WEEK( ADDDATE( `date_column` , WEEKDAY(NOW()) ) )
SELECT users.* from users
WHERE created_at >= '2011-12-01'
AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)
This selects the users created between 201-12-01 and 7 days after that.
Make changes, to query based on your need.
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);