jQuery flot, MySQL count rows for each day - php

I've table for hits that contains of 2 fields
id time(TIMESTAMP)
1 2012-05-03 08:56:17
2 2012-04-26 03:22:20
3 2012-05-03 08:56:17
How can I count how many rows for each day for example for the past 10 days?
I mean what is the right query for it that produces:
2012-05-03 => 25 hits
2012-05-05 => 55 hits
etc
Also, how can i arrange the returned array as jQuery flot array ?
any answers / articles regarding my questions would be great help
Thank you

You want to use a Group By on the date field
something like
select count(*), dateField from tablename
group by dateField

Here is how I'd do it:
select count(id), DATE(time) as hitday from yourtable group by hitday
And regarding your jQuery thing, I am not very sure if I understand it or not. You want mysql to generate a jQuery parsable array for you? For that you definitely need to have a kinda middle man like php.
Here is another thread which shows you how a select result is converted to json using php.

Related

SQL - Show months without data using group by

I've been searching without success for a way to list the data of all months, on format YEAR-MONTH, between two dates in a report.
I have this query that work well but doesn't show the months when doesn't exist data.
SELECT DATE_FORMAT(FROM_UNIXTIME(bt.date_submitted),'%Y-%m') AS month, count(*) as the_count
FROM bug_table bt
WHERE bt.category_id=2
AND bt.date_submitted BETWEEN " . db_prepare_string( $db_datetimes['start'] ) . "
AND " . db_prepare_string( $db_datetimes['finish'] ) . "
GROUP BY month
ORDER BY month ASC
The date_submitted is a UNIX_TIMESTAMP and $db_datetimes is a associative array on php that store the dates chosen by the user.
For instance, I want a list all months between 2016-10-01 and 2017-03-20. I got :
month | the_count
2017-03 7
2017-02 5
2017-01 2
2016-12 10
But I would like as below, including zero on months that have any record:
month | the_count
2017-03 7
2017-02 5
2017-01 2
2016-12 10
2016-11 0
2016-10 0
Sincerely I prefer to solve this problem on SQL, but any ideas using SQL or php will be welcome!
You can create a table with all months and do a left join from that table to your main table. And then replace NULLs with 0.
Have you tried to join them on the dates
FROM table1 LEFT OUTER JOIN date ON table1.date=table2.date
You can go ahead with one of the following approaches:
Create a list of month-year values dynamically (using a query) and LEFT JOIN result of that query to the one you are using. An example SO answer is here.
Create a calenar table and populate it with dates. You can then use the same LEFT JOIN with calendar and get the values.
Handle this logic in service/business layer of your application (i.e.create an array of month-year and show the values on User Interface based on results retrieved from db).
I would recommend using third approach as it's the cleanest one and won't add complexity to your db or service layer.

create mysql query for fetching data in 3 days slotwise

I want to create a query for fetch all the data from database. But condition is get from before current month and data will convert into 3 days slot wise with count.Like if i have 12 month data and in July 1 to 3 date data will insert into 50 rows and from 3 to 6 date data will insert into 5 rows.How i fetch this things.That is the mail problem. I have wondered but nothing found related my problem. Is this possible.
I assume you have a DATE or a DATETIME column, that marks the day of the insert, say the column created_at. Then you could get the desired result.
SELECT
COUNT(*)
FROM
your_table
WHERE
created_at BETWEEN '2014-07-01' AND '2014-07-31 23:59:59'
GROUP BY
(DAYOFMONTH(created_at) - 1) DIV 3
Explanation:
With the help of the DIV operator you get your 3-day-slots.
Note
If there's a slot without rows, it won't be in the result. To get these too, you could use a LEFT JOIN with the maximum 11 slots.
Demo
If you having timestamps as attribute type then might be this help you
SELECT count(*) as count
FROM table
AND
TIMESTAMPDIFF(DAY,date,2014-08-01)<=3";
The date is the attribute of your column, TIMESTAMPDIFF will find difference between given date if it's under 3 days from 2014-08-01 records will show up. This way by just sending one date into this position you can find 3 slots of date from your table.

How do you count the distinct values for each day using a timestamp column?

I'm using mysql and I'm having trouble thinking of a query to count the number of users/visitors for a certain date range. The way that I'm currently doing it is using php, I select the date range and process the data in a for loop and then just count them there. It's actually pretty easy, but the problem is that this method does not work for bigger data of a few million rows. The alternative is to count the distinct values using mysql only and just return a count and not actual data by utilizing the index on the timestamp column. Also, converting the column to a datetime is not an option. Any ideas how I can achieve this?
Here's a sample result set of what I need:
date | count
5-01-13 14
5-02-13 44
5-03-13 23
5-04-13 13
My problem is that I don't know how to group the timestamp column by day.
That should do the trick:
SELECT DATE('datetimecolumn'), COUNT(*)
FROM Table
GROUP BY DATE('datetimecolumn')
You just have to do the same, but instead add a group by clause:
SELECT myDate, count(distinct myField) as cnt
FROM myTable
WHERE myDate BETWEEN ? and ?
GROUP by myDate;
Where the "?" are the dates you use in your original query.

php mysql date extract with distinct

in mysql table i have the field 'date' which has the data as
2012-08-02 02:33:26
2012-08-02 05:33:26
2012-07-02 06:33:26
2012-06-02 01:33:26
2012-06-10 09:33:26
2011-05-10 10:33:26
2011-04-10 02:33:26
like that
i want to get the output as
2012-08
2012-07
2012-06
2011-05
2011-04
ie get the unique year and month (distinct of year and month of the date field)
I used the following query
SELECT distinct(YEAR(date)) FROM table
It returns the year only. i want to get the output as defined above. is there any possibility to get the out put like this. How do i write the select query.
Please help to this.
You might want to try this:
SELECT DISTINCT DATE_FORMAT(colName, '%Y-%m')
FROM tableName
SEE HERE: Date_Format( )
SELECT DISTINCT DATE_FORMAT(`date`, '%Y-%m') FROM table
you can get full help from here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
suppose you are getting like this from Database:-
$row['date']=2012-08-02 02:33:26;
then you can use explode function:-
$var=explode(" ",$row['date']);
$date=$var[0];//here you will get 2012-08-02
$res=explode("-",$date);
$res1=$res[0];//you will get 2012
$res2=$res[1];//you will get 08
$res3=$res[2];//you will get 02
now the final result:-
$final=$res1."-".$res2;//you will get 2012-08
this is an alternate answer if you don't want to handle this on MySQL end so you can handle this by using PHP concepts.

Returning the Quarter in SQL Server

I am a complete novice at SQL Server and naively thought that there would be a QUARTER() function, alas there is not and some googling didn't come up with anything useful!
Basically what I want to achieve is that for all rows in my database I want a count of those rows grouped by Quarter.
If possible I would like to keep all calculation with the query but if not it is PHP that is kicking everything off.
This should do the trick, provided that you have a datetime column in the row of course :)
SELECT datepart(qq, my_date_column) as quarter, count(*) as rows
from my_table
group by datepart(qq, my_date_column)
Of course, if you have more than one years data, you might want to add datepart(yyyy, my_date_column) as well
I use
select Datename(quarter,MYDATECOLUMN_NAME)
MYDATECOLUMN_NAME is date format.
I simply used the Month Function on the date and then used a Case function to organize them:
Case When Month(date) in (1,2,3) then '1stQtr'
When Month(date) in (4,5,6) then '2ndQtr'
When Month(Date) in (7,8,9) then "3rdQtr'
When Month(Date) in (10,11,12) then '4thQty' END AS Quarter
select
CONVERT(DATETIME, CONVERT(CHAR(8),
DATEPART(YEAR, yourDateColumn) * 10000 +
( DATEPART(QUARTER, #date) * 3 - 2) * 100 +
1),
112) from...
taken from here:
http://geertverhoeven.blogspot.com/2007/01/get-quarter-of-given-date-in-datetime.html
Or you could the SQL mentioned in the comment in that link, to get the first day of the quarter:
DATEADD(qq,DATEDIFF(qq,0,#date),0)

Categories