Get number of entries grouped by X points in time in MySQL - mysql

I need to build the backend for a chart, which needs to have a fixed amount of data points, let's assume 10 for this example. I need to get all entries in a table, have them split into 10 chunks (by their respective date column) and show how many entries there were between each date interval.
I have managed to do kind of the opposite (I can get the entries for a fixed interval, and variable number of data points), but now I need a fixed number of data points and variable date interval.
What I was thinking (which didn't work) is to get the difference between the min and max date from the table, divide it by 10 (number of data points) and have each row's date column divided by that result and also grouped by it. I either screwed up the query somewhere or my logic is faulty, because it didn't work.
Something along these lines:
SELECT (UNIX_TIMESTAMP(created_at) DIV (SELECT (MAX(UNIX_TIMESTAMP(created_at)) - MIN(UNIX_TIMESTAMP(created_at))) / 10 FROM user)) x FROM user GROUP BY x;

Related

Calculating Retention using SQL

I am currently trying to calculate the retention rate (percentage of customers that returned to a webpage) of 3 days for in a entire table within the span of 14 days. To do so, I am trying to count the total user (visitorId) of whom returned to the page between specific dates, then I would average them together in order to have the the average retention rate for the 14 days. Currently I am using this code but it does not seem to work.
SELECT
pageviews.pageType,
pageviews.pageviewDate,
sessions.sessionDate,
sessions.deviceType,
sessions.visitorId
AVG(COUNT(sessions.visitor > 1 BETWEEN sessions.sessionDate '2018-04-26' AND '2018-04-29')
# There would be multiple of these dates
FROM sessions
INNER JOIN pageviews
ON sessions.visitorId = pageviews.visitorId AND
pageviews.pageviewDate = sessions.sessionDate
WHERE
pageviews.pageType = 'Game' AND sessions.deviceType = 'Desktop';
To be more specific, the desired result would be to have a single number that states the average number of customers that returned to a specific page (in this case, Game) that used Desktops. Can anyone help? Please let me know if more clarification is needed. Note, for simplicity, I did not add all the date that I would calculate the retention rate as it would be many.

mysql - calculating columns(2 columns with numbers) based on another group of column(text)

Hi I would like to find a query for the below, I am trying to calculate data between two columns however based on another column which needs to be a selected group of the same values
Unfiltered
Start Time________Disconnect Time______Signalling IP
12:59:00.3________13:26:03.3___________1.1.1.1
10:59:00.3________11:03:03.3___________2.2.2.2
19:59:00.3________20:02:03.3___________1.1.1.1
Filtered
Start Time________Disconnect Time______Signalling IP
12:59:00.3________13:26:03.3___________1.1.1.1
19:59:00.3________20:02:03.3___________1.1.1.1
If you see the table above, I want the selected IP only which is 1.1.1.1, and then from there, calculate the total duration of time from the Start Time and Disconnect Time for that Egress IP.
So column 3 has multiple values, however I need to select the same value, then from there calculate the sum of column 1 and 2 based on column 3.
Please let me know if you have anything in mind, as I have tried multiple queries but can't get the correct one
to calculate difference between to times.
you can use time_to_sec to convert each time value to seconds
and subtract start time from end time to get time period in seconds.
you cat turn it back to time format with SEC_TO_TIME
example
select
column3,
SEC_TO_TIME(sum(TIME_TO_SEC(column2) - TIME_TO_SEC(column1))
from
table
group by column3

MySQL Limit amount of records returned between 2 dates

I'm afraid I with this situation:
I have a MySQL table with just 3 columns: ID, CREATED, TOTAL_VALUE.
A new TOTAL_VALUE is recorded roughly every 60 seconds, so about 1440 times a day.
I am using PHP to generate some CanvasJS code that plots the MySQL records into line graph - this so that I can see how TOTAL_VALUE changes over time.
it works great for displaying 1 day worth of data, but when doing 1 week(7*1440=10080 plot points) things get really slow.
And a date range of for example 1-JAN-2016 and 1-SEP-2016 just leads to time outs in the PHP script.
How can I write some MySQL that still selects records between a date range but limit the rows returned to ie max 1000 rows?
I need to optimize this by limiting the number of data points that need to be plotted.
Can MySQL do some clever stuff where it decides to skip 1 every so many rows and return 1000 averaged values - this so that my line graph would by approximation still be correct- but using fewer data points?

Average between two columns in mysql that are date not timestamp

Alright so here it is. I need to figure out the average amount of days between two columns.
Column 1 is recieved_date and column 2 is fix_date
Just want to know how to take the two dates find the difference in days, do that for every row and pop out a number stating the average amount of days it takes to fix something.
Tried to find it online but every time I find something like it, they have two specific dates. I need the entire columns averaged.
You can use the TIMESTAMPDIFF function both for dates and datetime.
See Mysql average time between visits
Add a group by and some other columns to this and it should do the trick:
select
avg(fix_period)
from
(
select
datediff(fix_date, received_date) as fix_period
from some_table
) as a
;

Calculate average date difference between records in MS Access

I have a list on when items have been handed out. The table has the following structure:
primary key - autonumber itemname
itemid - number
datehandedout - date/time
I want to calculate the average length of time between when one object is given out and the next one is given out. There will be a number of different items for which the average time between handouts needs to be listed for.
So something like (pseudocode):
average( [thisrecord]![datehandedout] - [lastrecord]![datehandedout] )
Any help will be much appreciated.
This is a very slow query:
SELECT Avg(DateDiff("h",[datehandedout],(
SELECT TOP 1 datehandedout
FROM tbl tx
WHERE tx.datehandedout > t.datehandedout))) AS Difference
FROM tbl AS t
Add another Where statement to limit the number of records returned when you test, for example:
WHERE Year([datehandedout])=2010