I have a table with two columns Days and Status.
I have to populate Status based on another lookup table.
Min -- 4
Max -- 6
Range -- 5 and 10
At a point of time there can be min / max / range populated.
Based on that Range, Days must be filtered and Status is updated to either success or failure.
Example:
MAX - 6
Means Status is set to success for those records for which Days have a maximum value of 6.
Related
I'm trying to grab the max value in a column up to and including the current row. In the xample below, I've ordered the table by date. I would like to calculate the value of rollingMaxSource as shown. Starting from the top, it reports the max value found up to that point. I then compare the max value to the source value. If source is lower than max, I flag it, since with this ordering the value of source should only be equal to or higher than the previous row.
Can this be done in MySQL?
Account
Source
rollingMaxSource
flag
date
1
10
10
false
2022-05-01
1
10
10
false
2022-06-01
1
9
10
true
2022-07-01
1
10
10
false
2022-08-01
1
11
11
false
2022-09-01
Yes, this can be done with windowing functions, which requires MySQL 8.0:
SELECT Account, Source, rollingMaxSource,
Source < rollingMaxSource AS flag,
date
FROM (
SELECT Account, Source, date,
MAX(Source) OVER (ORDER BY date) AS rollingMaxSource
FROM mytable
) AS t;
This works when MAX() is used as a window function because the default frame includes from the earliest row to the current row.
I have a table which has the following columns
dt_id,date,sl1,sl2,sl3,sl4,sl5,sl6,sl7,sl8.................sl24
The above are the structure for a allotment system in which in a particular day there are 24 available slots from sl1 to sl24 all of which have a default value of 1 which means that it is unreserved . Is there any way in which i could calculate the number of fields which have the value 1 . So that i can get the number of slots which are available for a particular day ?. PLEASE CORRECT ME IF MY SCHEMA IS WRONG . This is my first code.
You should normalise your table to be in format:
dt_id,date,slot_id,slot_status
where for each date now you can define any number of slots (for current use case that can be 24)
Now to fetch the number of slots which are available can be done in simple query:
Select count(*) from table where slot_status = 1 and date = <your_date_here>
To get the available slots use:
Select * from table where slot_status = 1 and date = <your_date_here>
I've a table with lots of entries consisting of dates and a number.
For instance:
07.02.2016 - 12
06.02.2016 - 48
05.02.2015 - 24
...and so on.
Now I need to sum all of the values older than 2 months. For instance the 3rd entry (05.02.2015) will be added to the second (06.02.2016) and the second one should get the value 72 and the 3rd one should be deleted.
I'd like to know if there is some way to do this in mysql only?
Instead of writing the code for you, I'd like to merely give you some hints:
Identify which rows are older than 2 month and sum them up.
select sum(number) from table where date > curdate() + interval 2 months
or sth. similar will do.
Select the max. date of the entries that are smaller or equal to "now+2months".
Update that row with the value from step 1.
Delete the rows from step 1.
See here for details on date functions in MySQL.
This can be done in 2 statements (one for steps 1-3, one for the deletion).
I have a trouble to sum fields.
This is what I have: I have list of employees and two rows of values for each, I also have calculations in SQL of running total for the 8 and 16 weeks for each employee for each of two rows using windowing function. I have to group employees by branches they work and calculate sum of running totals for the last 8 and 16 weeks for each row and then device row 1 by row 2. I need to use Last function, because I only need the running total for the last 8 and 16 weeks. The challenge is to go around and have something like: Sum(Last(Fields!Last116WeekSilk.Value) . this one obviously gives me an error. I have tried to add calculated field to the dataset with both Sum and Last functions, doesn’t work, tried RunningValue, doesn’t work. What else can I do to have the sum of last running totals?
Many thanks in advance
I would add the SQL ROW_NUMBER function to the query and derive the row number (e.g. 1 or 2) within each group. If you do this in descending sequence the row number 1 will always be the last row within the group.
Then in SSRS you can use an Iif to only show/calculate on row number 1.
I have a MySQL table containing a column to store time and another to store a value associated with that time.
time | value
------------
1 | 0.5
3 | 1.0
4 | 1.5
.... | .....
The events are not periodic, i.e., the time values do not increment by fix interval.
As there are large number of rows (> 100000), for the purpose of showing the values in a graph I would like to be able to aggregate (mean) the values for an interval of fixed size over the entire length of time for which the data is available. So basically the output should consist of pairs of interval and mean values.
Currently, I am splitting the total time interval into fixed chunks of time, executing individual aggregate queries for that interval and collecting the results in application code (Java). Is there a way to do all of these steps in SQL. Also, I am currently using MySQL but am open to other databases that might support an efficient solution.
SELECT FLOOR(time / x) AS Inter, AVG(value) AS Mean
FROM `table`
GROUP BY Inter;
Where x is your interval of fixed size.
I've usually solved this through a "period" table, with all the valid times in it, and an association with the period on which I report.
For instance:
time day week month year
1 1 1 1 2001
2 1 1 1 2001
....
999 7 52 12 2010
You can then join your time to the "period" table time, and use AVG.