How to calculate the moving average without window function - mysql

I am struggling with calculating 5- and 10-day moving averages. I have these fields in my table: Sno, date, and price. Based on these I will get the 5- and 10-day moving averages. Below is the table.
(Please note that I am using an older version of MySQL, in which window functions are not supported.)
2 day average: it will be calculated at row no 3 as (row1+row2)/2, row 4 will be (row2+row3)/2 and so on..
4 day average: it will be calculated at row no 5 as (row1+row2+..+row4)/4, row 6 will be row2+row3+..+row5/4 and so on..
Final: Final is based on the below formula
IF(AND(D5>E5,D4<E4),"BUY",IF(AND(D5<E5,D4>E4),"SELL","HOLD"))
Example data:
Sno(A) Date(B) Close Price(C) 2 D_AVG(D) 4 D_AVG(E) Final(F)
1 01-01-13 2316.525 0 0
2 02-01-13 2304.89 0 0
3 05-01-13 2292.1825 2310.7075 0
4 06-01-13 2279.67 2298.53625 0
5 07-01-13 2267.8325 2285.92625 2298.316875 SELL
6 08-01-13 2254.735 2273.75125 2286.14375 HOLD
7 09-01-13 2129.906 2261.28375 2273.605 HOLD
8 12-01-13 2124.264 2192.3205 2233.035875 HOLD
9 13-01-13 2119.432 2127.085 2194.184375 HOLD
10 14-01-13 2114.34 2121.848 2157.08425 HOLD

Related

How to reduce redundant cells in a column containing logged data

Is there a function to reduce the amount of redundant data from one column to match the number of cells in a second column?
I have logged data from two sensors that sent values at different rates. in 8 hours, I collected 11857 values for the first sensor and 8130 for the second one.
I need to compress the first column by deleting data to match the number of cells on the second column, so I can display synchronized values on a chart.
It is not a matter of cutting 3727 cells from the head or tail of the first column, but to delete cells in a proportional way.
I've tried using de Modulus function, but it does not give me the right amount of compression; e.g., by running =MOD(A1,3) and then filtering cells containing '0' value and deleting those rows, I get 7905, which is close to 8130 but still, the data is shifted out.
Edit:
I found a method that requires several steps:
Copy the sensors' data into two columns
Get the number of cells for both columns using COUNTA
Get the ratio between the smaller count over the bigger count
In a new column, create an index for the rows using =INT(ROW()*ratio)
Remove duplicate rows using the index column as the reference with Data > Remove Duplicates
It works, but it will be much faster if there was a ready-made function that will run over the provided data columns and copy the values into two new columns
I tested this solution in LibreOffice Calc. The functions used are basic enough to be found in Excel as well.
Here's a sample with data from 2 sensors, s1 and s2, similar to yours:
Row s1 s2
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
6 12 18
7 14 21
8 16
9 18
10 20
11 22
What I did was match the data from s1 samples with those from s2 that relatively match the position of the first, so instead of ending up with a number of rows with no s2 values, I padded non-existent s2 values with the last sample taken for any given period of time (column s2a)
Row s1 s2 s2a
1 2 3 3
2 4 6 6
3 6 9 6
4 8 12 9
5 10 15 12
6 12 18 12
7 14 21 15
8 16 18
9 18 18
10 20 21
11 22 21
Assuming that s1 is column A and s2 is column B in the spreadsheet, the function you want on each cell of the new column is:
=INDIRECT( ADDRESS( CEILING( ROW()* COUNT(B:B)/COUNT(A:A)),2))
Let's go from bottom to top:
COUNT(B:B)/COUNT(A:A) - this is the ratio. 0.63' above. It indicates that each sample in any given row in s1 will be found at that row x 0.63 in column s2.
Ceiling - Spreadsheets don't start at row 0, so the first one HAS to be 1. I experimented with Int(), but if the ratio were less than 0.5 we would end up with a 0, which we don't want.
Address - Returns a string with the address of a cell given its row,column coordinates (e.g. Address(3, 2) = "B3" and Address(3,2,2) as used here, will yield an absolute column or "$B3").
Indirect - Returns the contents of a cell whose address is passed as a string (e.g. Address("x5") will return whatever value is stored in cell X5).
Alex

Query that returns quantity of repeated values

In mysql, I need a query that returns the quantity of repeated values in the field "Info" of my table "Log".
Table Log:
ID_Log User Info
1 1 3
2 1 3
3 1 3
4 1 5
5 1 6
6 1 6
7 1 7
8 1 8
9 1 8
The query should return "4" (Info 3 appears three times, Info 6 appears two times, Info 8 appears two times).
Any suggestions?
You can get the number of values that have already appeared by using a simple subtraction. Subtract the number of distinct values from the total number of rows:
select count(*) - count(distinct info)
from log;
The difference is the number that "repeat".
This should work. Group the values of info together and only keep the results where the number of occurrences minus 1 is greater than 0. Then sum the numbers of occurrences.
select sum(repeats)
from (SELECT Info, count(*) - 1 AS repeats
FROM Log
GROUP BY Info
HAVING repeats > 0)

SSRS - Sum each cell value into next column

SSRS Question - Is there a way to sum each cell value into
the next colum. Here's what I'm trying to achieve. Colm B displays
the sum of colum A upto that row
Col A Col B
1
1
2
3
3
6
4
10
5
15
6
21
7
28
8
36
9
45
You want running totals. Everything you need is here.
Basically it will take each value from a data set and sum it up with the total from all previous values.
Some basic syntax: =RunningValue(Fields!A.Value,Sum,"yourDataSet")

Add together grouped rows into one value

I've got an issue where I've been presented data in this format from SQL, and have directly imported that into SSRS 2008.
I do have access to the stored procedure for this report, however I don't want to change it as a few other reports rely on it.
Project HoursSpent Cost
1 5 45
1 8 10
1 7 25
1 5 25
2 1 15
2 3 10
2 5 15
2 6 10
3 6 10
3 4 5
3 4 10
3 2 5
I've been struggling all morning to understand how/when I should be implementing the SUM() function with this.
I have tried already to SUM() the rows, but it still outputs the above result.
Should I be adding any extra groups?
Ideally, I need to have the following output:
Project HoursSpent Cost
1 25 105
2 15 40
3 16 30
EDIT: Here is my current structure:
"LineName" is a group for each project
You have to add a row group on "Project" since you want to sum up the data per each project.

SSRS Tablix report, handling column period totals and subreport comparisons

I'm new to SSRS (2008) and am trying to replicate an existing Access report. The report lists sales totals by month, and I've not had any issue resolving the basics into a tablix. However the original Access report then totals columns by quarter, 6 month and yearly values, and moreover applies incorporates subreports to compare these with previous year totals and targets. Schematically thus
Sale Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Total
Customer 1 1 11 10 8 1 2 0 0 0 1 3 4 40
Customer 2 0 1 3 1 0 0 0 1 1 0 2 1 10
MonthlyTotals 1 12 13 9 1 2 0 1 1 1 5 5 50
Quarterly 26 12 2 11
6 Monthly 38 13
Yearly 51
Prev Yr Totals 2 10 10 5 5 0 0 0 0 0 0 10
Monthly Diff -1 2 3 4 -4 2 0 1 1 1 5 -5
Quarterly Diff 4 2 0 10
And so forth. Note that the parameters are set so that the report can start at any month to lists the columns (for different financial years)
I have everything working fine for the first 4 lines (sale, customer 1, customer 2 and monthly totals) in the above, but cannot see the best way of
Displaying the quarterly etc totals
Displaying the subreports to show the previous years and target values with the differences between them and the current values.
I have full access to the SQL Server and am comfortable with complex queries and stored procedures, so was inclined to generate the values in a table and display out that, but is there a better way? In particular handling the quarterly etc totals in SSRS would be advantageous.
I think it is not possible to caclulate the quarterly totals with the help of Tablix alone, unless your SQL Table has a separate column named "Quarter".
So to achieve your requirement, you have to write simple stored-procedure which returns the resultant table along with one more additional column named "Quarter" which contains the values from "Q1..Q4" which is calculated based on the sales month field.
Then you can add this new column (Quarter) as ColumnGroup on top of your existing group in the tablix.
Hence the whole idea is to create the result set with all possible columns for which you want to group.