I need to count no of records in a table daily between 9 AM to 6 PM on a given date range.
Sample output should look like:
Date Count of users
01-11-2018 100
02-11-2018 88
03-11-2018 107
04-11-2018 113
SELECT DATE(date), count(*)
FROM table
WHERE TIME(date) BETWEEN TIME('9:00:00') AND TIME('18:00:00')
GROUP BY DATE(date)
SELECT `date`,
Count(*)
FROM table_name tn
WHERE tn.date >= Timestampadd(hour, 9, Curdate())
AND tn.date <= Timestampadd(hour, 18, Curdate())
GROUP BY `date`
I am running the following mysql query:
SELECT visitnum, userid
FROM user_visit
WHERE date >= '2015-10-31 00:00:00' AND date <= '2015-11-01 23:59:59'
Which returns me the following results:
visitnum userid
2010 60265
2011 60264
2012 60264
2013 60268
2014 60269
2015 60269
2016 60269
As you can see, this means the user 60265 and 60268 has one visit; user 60264 has two visits and user 60269 has three visits.
Now - how do I modify my mysql query so that it returns me only the rows associated with users that only visit ONCE? In other words, I expect my query to return me the following result:
visitnum userid
2010 60265
2013 60268
And how do I modify the query to return me only the rows that associated with users that only visit TWICE? like this:
visitnum userid
2011 60264
2012 60264
SELECT visitnum, userid
FROM user_visit
WHERE userid IN (
SELECT userid
FROM user_visit
WHERE date >= '2015-10-31 00:00:00' AND date <= '2015-11-01 23:59:59'
GROUP BY userid
HAVING COUNT(*) = 2
)
You can use this trick:
SELECT max(visitnum) as visitnum, userid
FROM user_visit
WHERE date >= '2015-10-31 00:00:00' AND date <= '2015-11-01 23:59:59'
GROUP BY usserid
HAVING COUNT(*) = 1;
The trick here is that MAX(visitnum) is the one-and-only visit number, when there is only one row in the group.
An alternative way that doesn't use GROUP BY is:
select uv.*
from user_visits uv
where not exists (select 1
from user_visits uv2
where uv2.userid = uv.userid and uv.visitnum <> uv2.visitnum
);
This should have better performance, if you have in an index on user_visits(userid, visitnum).
I have a table event, where i have records with a field end_date, so my problem is i want to fetch number of records, grouping month wise, where end_date should with in that month only, so for example:
If a record have end_date as 2013-01-01 00:00:00 then it should be counted in January 2013, and i am not able to do that. I am unable to put that where condition, how to do tell database that end_date should be between the month for which it is currently grouping.
SELECT COUNT(*) AS 'count', MONTH(created) AS 'month', YEAR(created) AS 'year' FROM event WHERE is_approved =1 GROUP BY YEAR(created), MONTH(created)
Please help me out.
EDIT :
Data say i have is like:
Record name end_date
record_1 2013-11-01 00:00:00
record_2 2013-11-30 00:00:00
record_3 2013-12-01 00:00:00
record_4 2013-12-04 00:00:00
record_5 2013-12-06 00:00:00
record_6 2013-12-10 00:00:00
...many more
Result Expected is:
Count month year
2 11 2013
4 12 2013
....so on
Try this:
SELECT COUNT(1) AS 'count', MONTH(end_date) AS 'month', YEAR(end_date) AS 'year'
FROM event
WHERE is_approved = 1
GROUP BY EXTRACT(YEAR_MONTH FROM end_date);
OR
SELECT COUNT(1) AS 'count', MONTH(end_date) AS 'month', YEAR(end_date) AS 'year'
FROM event
WHERE is_approved = 1
GROUP BY YEAR(end_date), MONTH(end_date);
::EDIT::
1. end date is greater than that particular month - Simply add where condition in your query and pass particular month in format of YYYYMM instead of 201411
2. event is started - Add one more where condition to check whether the created date is less then current date
SELECT COUNT(1) AS 'count', MONTH(end_date) AS 'month', YEAR(end_date) AS 'year'
FROM event
WHERE is_approved = 1 AND
EXTRACT(YEAR_MONTH FROM end_date) > 201411 AND
DATE(created) <= CURRENT_DATE()
GROUP BY EXTRACT(YEAR_MONTH FROM end_date);
OR
SELECT COUNT(1) AS 'count', MONTH(end_date) AS 'month', YEAR(end_date) AS 'year'
FROM event
WHERE is_approved = 1 AND
EXTRACT(YEAR_MONTH FROM end_date) > 201411 AND
DATE(created) <= CURRENT_DATE()
GROUP BY YEAR(end_date), MONTH(end_date);
The count is aggregated based on the month and year so if you are spanning years, you wont have Jan 2013 mixed with Jan 2014, hence pulling those values too and that is the same basis of the group by.
As for your criteria, that all goes in the WHERE clause. In this case, I did anything starting with Jan 1, 2013 and ending Dec 31, 2014 via 'yyyy-mm-dd' standard date recognized format. That said, and the structure of the table you provided, I am using the "end_date" column.
SELECT
YEAR(end_date) AS EventYear,
MONTH(end_Date) AS EventMonth,
COUNT(*) AS EventCount
FROM
event
WHERE is_approved = 1
and end_date between '2013-01-01' and '2014-12-31'
GROUP BY
YEAR(end_date),
MONTH(end_Date)
Now, if you want them to have the most recent events on the top, I would put the year and month descending so 2014 is listed first, then 2013, etc and months within them as December (month 12), before the others.
GROUP BY
YEAR(end_date) DESC,
MONTH(end_Date) DESC
Your criteria could be almost anything from as simple as just a date change, approved status, or even get counts per account status is so needed, such as (and these are just EXAMPLES if you had such code status values)
SUM( is_approved = 0 ) as PendingEvent,
SUM( is_approved = 1 ) as ApprovedEvent,
SUM( is_approved = 2 ) as CancelledEvent
Per comment feedback.
For different date ranges, ignore the between clause and change the WHERE to something like
WHERE end_date > '2014-08-01' or all after a date...
where end_date < '2014-01-01' or all before a date...
They will still group by month / year. If you wanted based on a start date of the event, just change that column in instead, or do IN ADDITION to the others.
MySQL has a bunch of date and time functions that can help you with that. For example:
MONTH() Return the month from the date passed
or
YEAR() Return the year
So you can just get the month and year of your dates. And group your results by them.
SELECT
COUNT(*) cnt
,MONTH(end_date) month
,YEAR(end_date) year
FROM events
GROUP BY month, year
Result :
cnt month year
2 11 2013
4 12 2013
Update:
For filtering only the records that have an end_date greater than a particular month AND have already started, you just need to add a WHERE clause. For example, if the particular month were February 2015:
SELECT
COUNT(*) cnt
,MONTH(end_date) month
,YEAR(end_date) year
FROM events
WHERE end_date >= '2015-03-01'
AND created < NOW()
GROUP BY month, year
Alternatively, the first part of the WHERE clause can be rewritten in the following way, which is probably more comfortable to use if you have to pass the year and month as distinct parameters.
...
WHERE (YEAR(end_date) > 2015
OR (YEAR(end_date) = 2015 AND MONTH(end_date) > 02))
AND created...
SELECT COUNT(*) AS 'count', MONTH(created) AS 'month', YEAR(created) AS 'year' FROM event WHERE is_approved =1 and month(created) = "the month u want" and year(created) = "the year you want" group by GROUP BY YEAR(created), MONTH(created)
you will need to pull the month and year... i could help with that but not sure how you are getting it but months would be 01/02/03 ect and year is 2013/2014/2015 ect
I have a record set of sales amount daily with different branches.
For example
Date Amount Branch
01/01/2014 30 A
01/01/2014 30 B
01/02/2014 40 A
01/02/2014 40 B
01/03/2014 30 A
01/03/2014 30 B
up to feb,mar,apr,may,jun,jul,aug
What i want to achieve is to group the record monthly based on todays date day.
For example today is 08/11/2014. the range should be 1st day of the month "1" then i will pick the day today which is 11. So the range for all the months is 1-11. See below sample.
Date Range for query monthly
01/01/2014-01/11/2014
02/01/2014-02/11/2014
03/01/2014-03/11/2014
04/01/2014-04/11/2014
05/01/2014-05/11/2014
06/01/2014-06/11/2014
07/01/2014-07/11/2014
08/01/2014-08/11/2014
Group this date range and get the sum of total sales.
Please help
This should do most of the work:
SELECT MONTH(date), SUM(amount)
FROM table_name
WHERE DAY(date) <= DAY(CURDATE())
AND date >= YEAR(CURDATE())
GROUP BY MONTH(date);
UPDATE
For the 3 letter month tag, also you'll probably want an ORDER BY to be sure:
SELECT DATE_FORMAT(date,'%b'), SUM(amount)
FROM table_name
WHERE DAY(date) <= DAY(CURDATE())
AND date >= YEAR(CURDATE())
GROUP BY MONTH(date)
ORDER BY MONTH(date);
You should be able to achieve what you want by using the following MySQL query:
select sum('amount') from 'some_table'
where dayofmonth('sell_date') >= 1
and dayofmonth('sell_date') < dayofmonth(currdate())
group by month('sell_date');
I hope it works, did not have some database to test.
You could eventually also group by branch, by adding an additional , 'branch' before the query's semicolon.
I have a query in which I would like to return the number of users who have logged in for the month without repeating the record in the next month.
If a user has logged in April and May, it only shows one record for April. This is what I have so far.
SELECT DISTINCT (a.userid), EXTRACT(MONTH FROM a.loginTime) as month
FROM login_audit a LEFT JOIN user u on u.userid = a.userid
WHERE a.loginTime <= '2012-12-31 11:59:59'
AND a.loginTime >= '2012-01-01 00:00:00'
GROUP BY month
So far the records are returning
userid month
1 1
2 1
1 2
3 2
In this scenario, user 1 is coming up for both January and Februray. I would like it to ommit that record. Either that or have it accumulated. Like so:
Either
userid month
1 1
2 1
3 2
Or
userid month
1 1
2 1
1 2
2 2
3 2
I hope this made sense. Please ask me anything if you'd like any further clarifications. Thanks a lot!
Don't see where you need table user...
For first "wanted scenario" :
SELECT
a.userid,
MIN(EXTRACT(MONTH FROM a.loginTime)) as month
FROM login_audit a
WHERE a.loginTime <= '2012-12-31 11:59:59' AND a.loginTime >= '2012-01-01 00:00:00'
GROUP BY a.userid
I would use this approach.
SELECT DISTINCT (a.userid), EXTRACT(MONTH FROM a.loginTime) as month
FROM login_audit a
WHERE a.loginTime <= '2012-12-31 11:59:59'
AND a.loginTime >= '2012-01-01 00:00:00'
and not exists
(select userid
from login_audit
where login_audit.user_id = a.user_id
and carry on with date range for the following month
)