I am working with a database that also has its build-in BI tool. My work involves the following stages:
Step 1: Write SQL query (let's suppose I extract two columns, date and average daily revenue)
Step 2: Use the retrieved data to create a visualization
Step 3: Add the visualization to the dashboard
I am writing the following query:
SELECT date, AVG(revenue) as revenue
From table1
Group by date;
I want the dashboard to be dynamic which needs the SQL query to be dynamic first.
The starting date is fixed, Oct 1, 2019 for example, but the ending date should be dynamic i.e. it should capture the data after every one day so that the dashboard gets updated daily.
How should my SQL query look like to achieve this purpose?
Use DATE_ADD to add a day as INTERVAL to start date.
SELECT rev_date,AVG(revenue)
FROM table1
WHERE rev_date BETWEEN "2020-06-15" AND DATE_ADD("2020-06-15",INTERVAL 1 DAY)
GROUP BY rev_date;
PS: Using DATE as column_name is not best practice,DATE being a datatype in MYSQL.
Related
I have to use a table data that I have to use in graphs in a format shown in picture below.
Demo graph
This is my data table below
sql data
My goal is to use filters on that, in which timestamp range will be same as shown in graphs, and I have to filter it based on system.
For example, on 28 January 2022, system "3IN1" got down 3 three, and on 29th January ,it got down 0 time and so on till a week. Then similarly for all the systems mentioned in image below. How to achieve it with a sql query and group by.
Try this... assuming start_timestamp is actually a TIMESTAMP data type
SELECT
system_name,
TO_CHAR(start_timestamp, 'YYYY-MM-DD') as date,
SUM(current_status) as current_status,
SUM(downtime) AS downtime
FROM
table_name
GROUP BY
system_name, TO_CHAR(start_timestamp, 'YYYY-MM-DD')
I am just learning MySql (SQL in general) and I have a question. I ran a process to populate a table with 72 records. This was done, however, I needed to run the process again and this time it populated the table again with a second record for each user for a total now of 144 records. How can I isolate the newest records created today?
A simple solution is to use current_date to figure out today's date and date() to remove the time portion of your column. Then:
where current_date = date(createdTS)
This is fine for a small dataset as yours. As general solution, you'd need a query that won't need to manipulate every row, e.g.
where createdTS >= current_date and createdTS < current_date + interval 1 day
You just have to use your createdTS column, (assuming you know what was the timestamp of both runs).
SELECT * FROM `my_table` WHERE `createdTS` > '2019-07-25 15:00:00'
You could also RANK() over and get only the newest run for each user (something like this)
I'm currently working on a project using vb.net and mysql database I just wanna ask how would I be able to sum up 2 or more entries of time in a column in mysql workbench formatted as hh:mm:sec.
For example I have this data stored on my database:
|03:45:00|
|03:12:00|
|03:08:57|
Sample Table
The total should be 10:05:02
I've tried to sum the entire column data using this query but I think I'm doing it wrong.
Select sum(Hours_worked) as Total
from db_project
where
Date_of_entry between '2019-02-04' and '2019-02-15'
order by
Date_of_entry asc;
You can't directly add time values to each other using SUM. Instead, you need to convert them to a value which can be summed (numeric) for example using TIME_TO_SEC. You can then convert the sum back to a time format (using SEC_TO_TIME and TIME_FORMAT) for display:
SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(Hours_worked))), '%H:%i:%s') AS Total
FROM db_project
WHERE Date_of_entry BETWEEN '2019-02-04' AND '2019-02-15'
Output:
10:06:02
Demo on dbfiddle
so i have a table with hundreds records. And a have a filed name "created" type with a datetime format. Now I want to make and archive with the months. For example January, February.... etc. I need to create query to find all possible months. For example if my records start from 2011/05/01 to now I will need to fetch the months that means months 5,6,7,8,9,10,11,12.
Is there a way to that ???
If you are looking at the list of all Months present in the created field (as I understand your query) then do this:
SELECT DISTINCT(MONTH(created)) FROM posts;
The resulting set would be the list of unique months in the field. If this will complain then try:
SELECT DISTINCT(MONTH(DATE(created))) FROM posts;
You can then substitute MONTH for MONTHNAME and get names instead. I did not add the WHERE clause to these queries but you can limit the dataset you are looking at as you see fit.
For more information take a look at http://dev.mysql.com/doc/refman/5.1/en/functions.html this has a list of quite a few functions that MySQL natively provides.
Yes, use the DATE_FORMAT function and other date and time functions.
More details here
For example, if you want all your records for December 2011:
SELECT * FROM posts WHERE YEAR(created) = 2011 AND MONTH(created) = 12
If I have MySQL query like this, summing word frequencies per week:
SELECT
SUM(`city`),
SUM(`officers`),
SUM(`uk`),
SUM(`wednesday`),
DATE_FORMAT(`dateTime`, '%d/%m/%Y')
FROM myTable
WHERE dateTime BETWEEN '2011-09-28 18:00:00' AND '2011-10-29 18:59:00'
GROUP BY WEEK(dateTime)
The results given by MySQL take the first value of column dateTime, in this case 28/09/2011 which happens to be a Saturday.
Is it possible to adjust the query in MySQL to show the date upon which the week commences, even if there is no data available, so that for the above, 2011-09-28 would be replaced with 2011/09/26 instead? That is, the date of the start of the week, being a Monday. Or would it be better to adjust the dates programmatically after the query has run?
The dateTime column is in format 2011/10/02 12:05:00
It is possible to do it in SQL but it would be better to do it in your program code as it would be more efficient and easier. Also, while MySQL accepts your query, it doesn't quite make sense - you have DATE_FORMAT(dateTime, '%d/%m/%Y') in select's field list while you group by WEEK(dateTime). This means that the DB engine has to select random date from current group (week) for each row. Ie consider you have records for 27.09.2011, 28.09.2011 and 29.09.2011 - they all fall onto same week, so in the final resultset only one row is generated for those three records. Now which date out of those three should be picked for the DATE_FORMAT() call? Answer would be somewhat simpler if there is ORDER BY in the query but it still doesn't quite make sense to use fields/expressions in the field list which aren't in GROUP BY or which aren't aggregates. You should really return the week number in the select list (instead of DATE_FORMAT call) and then in your code calculate the start and end dates from it.