Get the data from month wise different tables which are created dynamically - mysql

I have following tables:
vbSuccessful_630
vbSuccessful_631
vbSuccessful_632
...so on which are month wise tables. I need to get the data according to date range which will select the required tables dynamically.
SELECT SUM(Duration/60)
FROM vbSuccessful_632, vbSuccessful_631
WHERE FROM_UNIXTIME(connectTime/1000, '%Y-%m-%d %H:%i:%s') BETWEEN '2021-11-19 14:50:00' AND '2021-12-20 14:51:59';
Can anyone suggest how should I keep all required tables under FROM that is related to my search range.

Related

How to group by(mysql) a particular timestamp range to show data in graphs?

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

Which database method should I take to collect my monthly information

I have information to be collected monthly. same data columns but different content of course. I'm asking about which are the best way to make the user insert this data, should I make a database table for each month with the same columns, or should I make one table with one column to determine the month.
For example:
table: July
id|program_name|program_date|program_result
table: June
id|program_name|program_date|program_result
Or:
table: monthly_info
id|program_name|program_date|program_result|month
I'm asking which way is more efficient than the other.
Thank you
Create one table to save all your data with date.
table: monthly_info
id|program_name|program_date|program_result|date
Then you can query monthly data as below.
If your condition month parameter is integer. Use this query. (this will return all data matches to month August)
SELECT * FROM monthly_info WHERE MONTH(date) = 8
If your condition month parameter is string. Use this query.
SELECT * FROM monthly_info WHERE DATENAME(mm, date) = 'August'

SQL Statement to get daily totals

I'm storing some data in a table and I want to be able to display the total data points per day on the graph. So the first graph point might be 7 for Monday, Jan 1, 2013, and then 3 for Tuesday, Jan 2, 2013...etc.
I have full time/date stamps for each of my data points in my table of SQL type datetime.
My pseudo statement looks like this, but I'm concerned since I'm using the datetime data type:
SELECT
DATE(created_at) AS create_date
COUNT(id) AS total
FROM
data_table
GROUP BY
create_date
How can I get the total data points per day, regardless of the timestamp?
Try this
SELECT
DATE(created_at) AS create_date,
COUNT(id) AS total
FROM
data_table
GROUP BY
DATE(created_at)
Best would be to start a daily cron job that stores the number of the data points for every day. So you can every day count the number between let's say 24.00.00 to 23.59.59.
If you want to count them on the fly you might have slow requests on huge data amounts, since the grouping query cannot use table index.
But maybe you can add a new table column where you store just the date additionally to the timestamp.

Getting week started date using MySQL

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.

MySQL - Find date ranges matching a list of months

I have several rows in a table, each containing a start date and an end date. The user has a checkbox for each month of the year. I need to determine which rows contain a date range that includes any of the user's chosen months.
It's easy to check the start & end months by, for example, MONTH(start_date) IN ($month_list), but this approach won't match any months between the two dates.
So I suppose what I'm asking is: is there a way of obtaining the inclusive months from a date range purely in SQL?
I assume you would want to include data rows where the date range spans or intersects with the selected periods - in which case, I'd shove the user selected periods into a table and do a fuzzy join, something like.....
SELECT DISTINCT at.*
FROM a_table at, user_periods up
WHERE at.start_date<=up.end_date
AND at.end_date>=up.start_date
AND up.trans_id=$SOME_VAR
(the trans_id just allows the table to be used for multiple operations)
To minimise the effort here, the user_periods table should have an index on start_date and end_date, and similar for a_table.
Can something like this help?
WHERE
MONTH(start_date) < MONTH_YOU_ARE_CHECKING and
MONTH() > MONTH_YOU_ARE_CHECKING
If you need to check all at once you can do a list of all the months and after delete from the list the month that the user choose, and after compare against the list. It will be better with a pseudocode example :)
MONTHS = 1,2,3,4,5,6,7,8,9,10,11,12
USER_SELECTED_MONTHS= 1,6,8,9,12
LIST_TO CHECK = 2,3,4,5,7,10,11
so, now you can do:
MONTH(start_date) NOT IN (2,3,4,5,7,10,11)
What do you think, could it help you?
regards