Show new rows per day in Grafana from MySQL table - mysql

I have a fresh Grafana setup which is connected to a single MySQL data source. I'm trying to create a simple Bar Gauge which has vertical bars, one for each of the last 10 days where the values is the total number of rows added on that day, based on the "created" column.
My table is simple...
id, int
created, datetime
name, string
I have the following query which I've built using the Query Builder...
SELECT
$__timeGroupAlias(created,1d,0),
max(id) AS "Brands"
FROM brands
WHERE
created >= CURDATE() - INTERVAL 30 DAY
GROUP BY 1
ORDER BY $__timeGroup(created,1d,0)
Works great however, it's showing the total number of rows i.e. the count is increasing each time. It should instead only show the number of rows created on that day.
01|03|05|08|12
| | | |==
| | |==|==
|==|==|==|==
==|==|==|==|==
It's worth mentioning that I've got the "Format as" set to "Table". If I set it to "Time Series" then my chart goes blank and has no bars at all.
And secondly, the labels beneath the bars all say "Brands" - I'd like to replace this with the date.
Any help would be greatly appreciated!

Related

MySQL: Extract data for line graph to show counts over time period

I have a table which captures when certain events (say alien attacks) happened. Each time the aliens attack a new record is created in this table (some days can have multiple attacks and some days none)..
attack_id attack_date
--------- ---------
1 03/12/2015
2 03/12/2015
3 04/01/2015
4 04/21/2015
5 06/14/2015
I want to show in a line graph how many attacks occured per week. So the x-axis would be weeks in the year and the y-axis would be the number attacks in that week.
Thus the result set to feed my graph might look like
Week Number of attacks
---- -----------------
Can someone suggest a mysql query?
two things you need: week() function to get the week from the date, and count() to get how many attacks happened:
SELECT WEEK(alien_date) as attack_week, COUNT(*) as num_of_attacks
FROM yourTable
GROUP BY WEEK(alien_date)

mysql query dynamic date range

I've created a website showing the prices of the fruits and vegetables of my province. These prices are updated every day (aprox. +250 rows each day).
For that reason I created a MySql table to speed up the access to this information.
On the table showed on my Web site I want to limit the date showed up to 1 week and not all the dates available on the database. Taking in consideration that the database is increased daily I need a query for this dynamic date request.
They query I'm using right now is:
SELECT base_beta.Tipo,
base_beta.fecha_numero,
base_beta.Variedad,
base_beta.Fecha,
base_beta.alhondiga,
base_beta.corte_uno,
base_beta.corte_dos,
base_beta.corte_tres,
base_beta.corte_cuatro,
base_beta.corte_cinco,
base_beta.corte_seis,
base_beta.corte_siete,
base_beta.corte_ocho,
base_beta.corte_nueve,
base_beta.corte_diez,
base_beta.corte_once,
base_beta.corte_doce,
base_beta.corte_trece,
base_beta.corte_catorce,
base_beta.corte_quince
FROM base_beta
WHERE 1=1
AND base_beta.Tipo = 'pi'
ORDER BY base_beta.fecha_numero DESC
There are serveral columns with corte (aka prices) because every item has a random number of prices, since the fruits and vegetables has a bid system where prices decrease on every round.
For example, a tomato may start at 0,80€ and then its prices decrease to 0.76, 0.74, 0.69 and so on.
What code do I have to add to show the data of the current day and the next 6 days to complete a week?
Here are a photo of the: database content and format
Thanks, Jose.
If this defines your table schema
CREATE TABLE prices (item INT UNSIGNED, price DECIMAL(5,2), date DATETIME);
the following query will retrieve prices up to 7 days back in time
SELECT * FROM prices WHERE date > SUBDATE(CURDATE(),7) AND date <= CURDATE();
See also https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Hope this helps.

Get Daily Active Users for the last 2 weeks

I have a log on our app that logs all user activity along with a unix timestamp and I now want to create a function that will return the data needed to display the number of DAU each day in the last 2 weeks. Ive googled around a bit , but I have been unable to find a straightforward question on Stack regarding how to even start to go about such a query . Maybe my querying knowledge just is not advanced enough , excuse my ignorance if this is the case. I just have no idea how to group individual dates + unique user totals in a single query.
my table is setup as following
|LOG_ID | TYPE_OF_REQUEST | USER_ID | TSTAMP |
You can start by filtering
WHERE TSTAMP <= DATE_SUB(NOW(), INTERVAL 2 WEEK))
To filter only the two last weeks
And then
GROUP BY USER_ID
Perhaps combining a COUNT(TYPE_OF_REQUEST)
Depends on what info you are trying to achieve

Mysql performance selecting blog post views by date

Question about SQL performance when selecting a 'blog post' based on user views by date.
I want to record the user views of each post, and i ll select everyone of them using 'daily' and 'monthly' as parameters:
PS:
Most viewed posts of the day, or month.
To record the views, i created a table to insert, after every page load, the date of each view.
And them select them (count them) by DAY() and MONTH() when needed.
The problem here is, when the table or the amount of users requiring this information grows the select starts to be slower, due to the amount of rows(views) multiplied for the amount of posts.
One alternative that i thought was, create a table for daily records, and another table for monthly records, then on every page load the code checks if there is a row for the selected date, if the rows exist the script increment the views count on it, if it doesn't, the script insert the row with views count = 1;
Ps:
Daily Views
Post ID | Views | Date
1 | 898 | 2014-07-11
2 | 676 | 2014-07-11
1 | 333 | 2014-07-10
This way every post can have only one row per day.
Is there any better option? what do you think about my alternative? there is no need for my suggestion?
I think the best solution is:
Create a table with statistical data with fields:
id
date (store date m-d-y)
day
month
year
views (store number of visits)
page (store blog post)
One unique row per day, and update programmatically as needed.
Then you can make queries using day, month, year fields, even you can add weeknum field to make queries to obtain statistics grouped by weeks.
As addition you can add a second table to store the full date (m-d-y h:m:s) for each visit, you can add fields like browser, ip, etc... to this table.

Querying for status updates per day from an audit log of version control?

I am currently working on an audit log that keeps track of the version history of the various items i.e. tracks the actual changes along with a marker stating the type of change (created, updated or deleted).
Now with each item there is also a 'status' column showing the status of that item (open, agree, maybe).
Required query: Get the count of the status of items per day till now. So the output should look something like this:
day | status | count
---------------------
1 | open | 3
2 | open | 4
2 | maybe | 1
2 | agree | 2
3 | open | 2
3 | agree | 2
and so on. I've been struggling to frame this query from the audit log table (wc_audit_log) that looks like the image below. There are other columns but are mostly text and irrelevant for this query (IMHO :)
I've tried playing around with various combinations of group by and order by as well as the year, dayofmonth, month functions, but can't seem to wrap my head around how to frame this query. The trickiest part being the 'day' boundaries and duplicates with respect to version control. That is, it's entirely possible to have an item be updated multiple times without any status updates in the same day or transition through multiple statuses within the same day.
So in case of status based duplicates, the latest timestamped item would be selected. I.e. if an item was updated twice and the status was 'open' both the times, just pick the last one. Double counting is fine i.e. if the item was open and agreed on the same day it's okay for it to be counted in both places.
However, I'm still unable to figure out how to frame such a query. The image above should shows a part of the table for only those columns that are relevant but should also give an idea of the duplicates etc. involved making this a non-trivial query in my opinion.
PS: The items marked as deleted wouldn't be considered so aren't part of the table above. However, the above holds true even if the item was deleted but existed 'in the past'
I think this does what you want. It counts the number of wc_ids that have any given status on each day. It does not count duplicates within a day.
select extract(year from timestamp), extract(month from timestamp),
extract(day from timestamp),
status, count(distinct wc_id)
from a
group by extract(year from timestamp), extract(month from timestamp),
extract(day from timestamp), status
order by 1, 2, 3, 4
However, if there are duplicates across days, then the id gets counted twice with the same status on the two days.
I reread your description a couple of times. Isn't it just:
select datediff(now(), timestamp), status, count(distinct wc_id)
from foo
group by 1,2
You might try this:
SELECT `day`, status, COUNT(wc_id) as `count`
FROM
(SELECT DATE(timestamp) as `day`, wc_id, status, MAX(timestamp) as `max_time`
FROM table_name
GROUP BY `day`, wc_id, status) AS max_timestamp_per_wcid_and_status
GROUP BY `day`, status
ORDER BY `day` ASC, status DESC