MySQL - SUM() old entries - mysql

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

Related

How can I add a row for Unpivoted data that is at the hourly level and I want to display it at the 15 min interval

I am very new to SSIS so the answer to this might be very basic.
I have data that is coming in at an hourly level, meaning that a single record would have 25 columns (hour 1 - 25 with the 25th being null for DST) I have unpivoted the data to show a single column with the hour. I now need to display this data at the 15 min interval.
My plan is to duplicate the row of data for hour 1, say it is 8 and divide by 4 and now each 15 min interval would be 2
What can I do to accomplish this goal?
I have no clue where to start, looking for ideas.
I strongly suspect that this is a terrible approach to whatever it is you really want to do, but since you haven't asked about what you really want to do, I'll treat your question as academic.
If you have to do this in the dataflow, you could write a script transformation with a loop that creates 4 output rows for every input row and does the division.
Personally I would do this in a stored procedure that gets called after the data is initially loaded into a staging table on the destination server:
SELECT from the staging table, cross joined to a CTE with 4 rows, and dividing the value by 4.

mysql Select Row by multiple conditions

We are working on a way to select 1 row based on multiple conditions. An example will clarify this. Below is an example of our data set.
Name StartDate EndDate HoursBegin HoursEnd RowID
Test 1 11/24/2017 8/24/2018 121 1000 1382
Test 2 11/25/2018 8/24/2020 1001 2500 1383
Test 3 11/25/2020 8/24/2022 2501 4000 1384
I am looking for a query that will take a couple of conditions (Date and Hours) and decide which row the condition fits it. Consider the following 2 examples.
Simplest case: Date: 11/25/2021 and Hours 2600. This should result in the the 3rd row being selected since both conditions are satisfied with the same row.
2nd case: Date: 11/26/2018 and Hours 2600. This should result in the 3rd row to be selected again. Our logic goes something like either by hours or date. For this I could not simple do an OR in my SELECT WHERE statement because the Date condition would be satisfied by the second row and that is not the row we want to bring back.
I have been trying to figure out how to do this for a couple of days now and have a brain freeze from thinking about it. Any help would be appreciated.
For this you need to use a where clause to filter out the rows you are interested in. Connect the clauses in the where clause with AND to signify you want all of them to be true for the row in question:
where startdate < '11/25/2021' and enddate > '11/25/2021' and
hoursbegin < '2600' and hoursend > '2600'
some sql platforms have a between operator
where '11/25/2021' between startdate and enddate and
'2600' between hoursbegin and hoursend

Querying Comma Delimited Values in MySQL

I need a way to query a collection of data. I have a list of recent activity dates each stored in 1 row per user. Each row has a field of loginDates which consists of a comma separated list of timestamps.
What i need to do is run reports on this date to find people active since XXXXXX timestamp. The problem is the fact it's comma separated means i can't query it uses methods i know.
Here is an example row
id userID accessDates
2 6 1399494405,1399494465,1399494525,1399494585,1399494623
What i want to achieve in plain text
SELECT all_fields FROM accessTable WHERE accessDate > YESTERDAY
ALSO These dates may however span over several hundreds of days with hundreds of timestamps in the field.
Assuming the TimeStamp values are in order as your data sample shows, if any of the TimeStamp values in the string are greater than a given date, then the latest one would be greater than that value as well. So you only need the latest TimeStamp value to meet your requirement:
SET #Yesterday =
UNIX_TIMESTAMP(DATE_ADD(DATE(CURRENT_TIMESTAMP()),INTERVAL -1 DAY));
SELECT *
FROM accessTable
WHERE CAST(RIGHT(accessDates,10) AS UNSIGNED) > #Yesterday;
If you want to query each of those TimeStamps individually, the best solution is to put them into a single table column with a userid:
userID accessDate
------ ----------
6 1399494405
6 1399494465
6 1399494525
6 1399494585
6 1399494623

sum of last field

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.

SSRS: Summing Static Rows Defined by Expressions

I am building a report that looks like:
Last 30 Days Last 60 Days Last 90 Days
SLA Met* 1 3 5
SLA Missed* 2 4 6
Total 3 7 11
The datasource is via FetchXML and only one dataset is being used for the table. I am calculating the static rows, SLA Met and SLA Missed, using the expresion:
=(Count(IIf Condition, 1, Nothing)).
Any ideas how I would get the total of these two static rows, as they do not belong to a group, not fields that I can easily sum, and are defined by an expression?
Thanks.
If I clearly understood the problem, the solution would be simple: = (expression) + (expression)
This amount must be in the same table.
You can create calculated fields with your condition, then use that data to create your total rows. They would then belong to the group and would be easy to sum up.