How can I query the total number of status per type of the current month and also query the total number of status of the future months to come. Both are separate queries. I looked up on the google but I had a hard time applying it.
I am using mysql.
my field names are
date status
format YYYY/MM/DD PENDING, ACCEPTED, REJECTED
I tried to use but get an error of SQL syntax
SELECT * FROM request WHERE (MONTH(date) = MONTH(GETDATE());
To get the statuses of the current month:
SELECT
status
COUNT(*)
FROM request
WHERE
MONTH(date) = MONTH(NOW())
GROUP BY status
To get next month's I would use:
SELECT
status,
COUNT(*)
FROM request
WHERE
MONTH(date) = MONTH(DATE_ADD(LAST_DAY(NOW()), INTERVAL 1 DAY))
GROUP BY status
We take the last day of the current month and add 1. Just to avoid using things like MONTH(NOW())+1 or DATE_ADD(NOW(), INTERVAL 30 DAY)
GETDATE() is a SQL Server function. Perhaps it will work with the MySQL function:
SELECT r.*
FROM request r
WHERE MONTH(date) = MONTH(now());
If this works, realize that it only refers to the calendar month in any year. It is not necessarily the current month.
Related
Assuming that there are 6 months of historical data with hundreds of rides per day:
Write a query that returns, for each of the last 90 days, a count of the rides taken in the 7 day window preceding that day
I would like to find a way to write this in MySQL but have had some trouble with having a rolling sum that resets along with how I could cut up timestamps to reflect a day of the year/date and to then group by that.
I have tried writing subqueries that will limit the sum to a week prior and then place an additional limit of 90 days after that but cannot seem to get the code to return any output.
I have tried writing this is PostgreSQL using a sort of "window" functionality but am much more comfortable working in MySQL and would like to be able to solve it that way. I am familiar on how to write limits, group and order among other things but I am having trouble with the rolling sum resetting per week.
Thank you for your help!
First you'll want a numbers table/query. There are some tricky CTE ways to do that but it might be easier for now just to add a table with the numbers 1-90 in 90 rows.
Then use that to generate, for each row, a date range. Sorry if the syntax isn't quite correct, but write a query along the lines:
SELECT num, DATE_ADD(CURRENT_DATE(), INTERVAL -(num+7) DAY) startdate, DATE_ADD(CURRENT_DATE(), INTERVAL -num DAY) enddate FROM numbers
Then you can cross-join that with your rides table grouped on num and counting the rows in the range:
SELECT num, startdate, enddate, SUM(CASE WHEN startdate <= ridedate AND ridedate <=
enddate THEN 1 ELSE 0 END) ridecount
FROM (date range query) dts, rides
GROUP BY dts.num
Hope that helps.
Assuming you have data on each day, a correlated subquery might be simplest approach:
select dt,
(select count(*)
from rides r
where r.ridedate >= d.dte - interval 7 day and
r.ridedate < d.date
) as rolling_7
from (select distinct ridedate as dt
from rides
) dt
I'm trying to request my database to get numbers of rows where a specific login appears from last 6 months ordered by weeks.
The output has to be something like this :
My request is like this :
SELECT count(*) FROM table1 where (`EndDate` BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 MONTH) and CURDATE()) AND the_login LIKE 'LOGIN 1';
It shows every rows from last 6 month for now.
Could you complete my request or change it if needed please ?
You can use YEAR() and WEEK() to extract them from the date, and group by them .
Note that I didn't use only WEEK because its not enough, if you'll perform this before June, weeks can be misunderstood.
Also, don't use LIKE for exact match, use them only for partially comparison.
SELECT YEAR(t.endDate) as m_Year, WEEK(t.endDate) as m_Week, count(*)
FROM table1 t
where t.`EndDate` BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
and CURDATE())
AND t.the_login = 'LOGIN 1'
GROUP BY m_Year,m_Week
I have player statistics which I would like to publish from certain dates.
At the moment I can see statistics in the database from the beginning.
SELECT name,
bankmoney AS Bank,
Playerkills AS 'Player Kills',
deathcount AS Deaths ,
aikills AS 'AI Kills',
teamkills AS 'Team Kills',
revivecount AS Revives ,
capturecount AS 'Territories Captured',
LastModified AS 'Last Seen'
FROM playerinfo
JOIN playerstats
ON playerinfo.UID = playerstats.PlayerUID
ORDER BY BankMoney DESC;
But I would like to present statistics from the start of the day and the start of the week.
How would I do that ?
Based on Spitfire's comments, to get the last 24 hours of data you can use INTERVAL and go back the required number of days you want. NOW() will give you the time the query was executed and BETWEEN will allow you to search between two days. So that part of the query could be:
WHERE 'Last Seen' BETWEEN NOW() AND NOW() - INTERVAL 1 DAY;
A week would require a change from 1 day to 7.
Try it.SELECT * from table_name where a.exam_date BETWEEN $date1 AND $date2;
for example if you want to see only statistics with lastModified greater or equal today just add a where clause:
WHERE 'Last Seen' >= CURRENT_DATE()
I put together a query for determining whether the time difference between the current date and a record from a database is exactly a month or more apart. I am comparing now() to a created_at column, which is a timestamp.
EX:
6-12-2014,
7-12-2014
AND
5-12-2014,
7-12-2014
Should be considered to be a desirable results.
SELECT count(*) FROM `subscriptions` WHERE
DATE_ADD(CAST(created_at as DATE),INTERVAL TIMESTAMPDIFF(MONTH, created_at, now()) MONTH) = CAST(now() as DATE);
However the query appears to not return all desired results. It returns 2-28-2014 and 7-28-2014, however it does not pull up 6-28-2014. Is there a better way of doing this than the solution I came up with?
Are you looking to count dates that are on the same day of the month as the current date? If so, try the DAYOFMONTH function:
SELECT COUNT(*)
FROM subscriptions
WHERE DAYOFMONTH(created_at) = DAYOFMONTH(NOW())
When I run the below query on the first on a month it returns no data. It is supposed to display the previous day.
The query works ok normally except when it’s the first on the month, am I doing something wrong which will not allow it to see the previous days data as it’s a previous month?
select COUNT(*) from osticket.ost_ticket where DATE(created) = DATE(NOW())-1
You need to use DATE_SUB :
select COUNT(*)
from osticket.ost_ticket
where DATE(created) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY));