Need daily count of whole month - SQL2008 - sql-server-2008

I am trying to generate simple report which gives me Failed test count daily for whole month. Is there a way to write a sql query that does that? Rather then running queries by changing dates? This is what i am doing:
select count (*) from students where Message Like 'Failed Test.%'
and Timestamp > '01-01-2013' and Timestamp < '01-02-2013'
So above query gives me count 5. Then i change the date from 01-01 to 01-02 and get next days count and so on. But this seems like very time consuming thing. Just checking if there is a better way to do that.

This gives counts grouped by day for a month's date range:
select cast(timestamp as date) as [Date], count (*) as NumFailedTests
from students
where Message Like 'Failed Test.%'
and Timestamp between '2013-01-01' and '2013-01-31'
group by cast(timestamp as date)
order by cast(timestamp as date)
You can obviously widen the date range...
[Hopefully the 'timestamp' column is not really a timestamp...]

Related

Database query on month

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.

Double group by SQL for day of week and hour/minute

Every minute I have a value in my database. Each record has a timestamp, and a value.
"30695","2015-09-06 18:10:09","693"
I want to get the average value for each minute for each day of the week, but I'm not sure how to build a query for this.
My thinking is that I first need to group all the records by DATEPART(weekday,[time]), which will put all my data into Sunday, Monday, Tuesday, etc. Next, I would group by minute, rounded. (So 2015/11/30 06:41:28 would get grouped with 2015/11/23 06:41:56 (both dates are Mondays)). Then, take the average of the value at each minute--7*24*60 = 10,080 total values.
There is no datepart() function in mysql, but you can use dayofweek() to get the day of the week and minute() to get the minute part:
select dayofweek(timestamp) as day, minute(timestamp) as minute, avg(value_field) as average
from table
group by dayofweek(timestamp), minute(timestamp);
Got it working, adapted from Shadow's answer, which wasn't quite right. Here's my query (MySQL 5.5)
SELECT weekday(timestamp) as day_of_week, avg(avg) as average, DATE_FORMAT(timestamp,'%H:%i') as hour_time
FROM audio
GOUP BY hour_time, day_of_week
ORDER BY `day_of_week` ASC

Find data of a whole month in sql

I have this query where I provide to-date & from date.
SELECT *
FROM sales
WHERE date between to-date AND from-date;
Now I want to execute this query with following parameters
to-date = Oct-2015
some-other-date = Oct-2015
That is I want records of the whole month.
How would I do that in a query where I have to and from dates provided it will work for both scenarios where months can be same and different as well.
Update:
dataType for column date is date
You can find the first day of the month containing any given timestamp with an expression like this. For example by using the timestamp NOW(), this finds the first day of the present month.
(DATE(NOW() - INTERVAL DAYOFMONTH(DATE(NOW()))
That's handy, because then you can use an expression like
(DATE(NOW() - INTERVAL DAYOFMONTH(DATE(NOW())) - INTERVAL 1 MONTH
to find the beginning of the previous month if you like. All sorts of date arithmetic become available.
Therefore, you can use an expression like the following to find all records with item_date in the month before the present month.
WHERE item_date>=(DATE(NOW()-INTERVAL DAYOFMONTH(DATE(NOW()))- INTERVAL 1 MONTH
AND item_date < (DATE(NOW()-INTERVAL DAYOFMONTH(DATE(NOW()))
Notice that we cast the end of a range of time as an inequality (<) to the moment just after then end of the range of time.
You may find this writeup useful. http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/
It's often useful to create a stored function called TRUNC_MONTH() to perform the conversion of the arbitrary timestamp to the first day of the month. It makes your SQL statements easier to read.
select * from sales
where from-date >= 1-Oct-2015
and to-date <= 1-Nov-2015
Update
select * from sales
where date >= from-date
and date <= to-date
Here is SQLFIDDLE
You Can get month from your both to and from dates and find records of that month
SELECT * FROM sales
WHERE MONTH('2002-01-03') AND MONTH('2002-01-3')
SqlFiddle of Using Month Function

Grabbing date records that are exactly a month or more away

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())

Query to limit number of requests per day

I am trying to limit the user to perform only 5 requests per day. To do that, I am counting the number of request already performed during the same day using the query below:
SELECT COUNT(*) FROM table_name WHERE date_field >= CURDATE()
This gives me requests already performed days before. All I need is only the number of request performed in the course today. Any idea please?
SELECT COUNT(*) FROM table_name WHERE date_field >= CURDATE()
... will return the number of rows where date_field is greater than or equal to today if date_field is a date datatype. That is, ensure time is not a component but it certainly cannot return "requests already performed days before" unless there is a bug in the value stored in date_field column.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Try:
SELECT COUNT(*) FROM table_name WHERE date(date_field) = CURDATE()
Assuming date_field is a timestamp/datetime field, date() will extract just the date part and evaluate it against the current date. You can read more about it on MySQL's Documentation