Mysql query data in time period
The table is shown in the figure, the table name is demo, and the table is a real-time update table of a certain inventory
The main data are: time data, amount inventory data
Mysql query data in the time period can use the between method
select amount
from demo
where time
between "2021-01-01 00:00:00" and "2022-01-01 00:00:00"
It should be noted that if the data on the right side of between is the same, it will not be counted, that is, open brackets
If the required data is only accurate to the day, and the parameters have no hour, minute and second, the data at the end of the time period will be lost, that is, the data on January 1 will not be counted.
You can use java's Calender to expand the scope
Calendar gc = Calendar.getInstance();
Date a=productionParam.getEndtime();
gc.setTime(a);
gc.add(Calendar.DAY_OF_MONTH, 1 );
gc.add(Calendar.SECOND,-1);
Add one day to the end data and subtract 1 second, which is "2022-01-01 23:59:59", and the data on January 1 can be counted
Process data on the same date
It can be seen that the date (accurate to the day) of the third and fourth items is the same
Processing can be divided into two cases, adding or choosing one of them
Addition can be done using sql's sum() function and group by
It should be noted that, because the time, minutes and seconds of the two data are different, group by cannot be directly grouped. You can use mysql's DATE_FORMAT to format and then group them. Since formatting and grouping cannot be done at the same time, a second query is required.
select
time,sum(amount)
from(
select
DATE_FORMAT(time, '%Y-%m-%d' ) AS time,
amount
from demo) as a
group by
a.time
Choose one of the same dates
Assuming that the data of the same date only needs the latest piece of data, you can use the MAX() method of mysql
Similarly, the date also needs to be formatted and grouped, but the amount of data after grouping is not controllable, and the most important thing is that the date cannot be distinguished after formatting. Which of the data of the same date is the latest, that is, the accuracy is lost
Therefore, the id field needs to be used . The default date is inserted in order, that is, the id is also arranged in order. At this time, the left join right join can be used to achieve this requirement.
select
id,
time,
amount
from demo
RIGHT JOIN (
select
MAX(id) as id
from(
select
id,
DATE_FORMAT(time, '%Y-%m-%d' ) AS time,
amount
from demo) as a
group by
a.time ) as b
on id=b.id
And then, in production
Demand changes, need to reflect the sum of each day's inventory in the time period, for statistics or line graphs
It needs to be classified again according to the type of data, and it is necessary to query the sum of all types of inventory each day.
The most important thing is to have a time period division, but the data outside the time period still needs to be considered (the sum of all types of inventory), and an initial value of the inventory before the start of the time period (the starting point of the line graph) is required.
can be understood as
To query the inventory in a certain time period, namely amount, you need to query the total inventory of each day in the time period. The total inventory of the day is the sum of the inventory of all types (types) on that day, and each data in the database is a Real-time updated data on inventory levels by category.
When querying, the latest data is selected when the same type of data appears on the same day, and when the total inventory quantity queried is not at the front end of the time period, the initial value needs to be queried, that is, the total inventory quantity before the time period, that is, when When the inventory of any type is not at the front end of the time period, the initial value before the time period needs to be queried and used for the subsequent total inventory calculation
At this time, the database query alone cannot meet the demand.
My workaround is
Query all the type numbers of the production data, put the initial value of the type before the query time period one by one and put it in the array, and then group by two fields (time and type) to get different types of real-time changing data, excluding the same type in the same The data of the day changes, and finally according to the date, type and inventory queried, the inventory value in the array is continuously replaced and calculated.
Can this problem be solved using only sql?