possible to calculate average - mysql

i have following sample database:
date open_price closed_price
25/1/19. 6 10
24/1/19 10 12
23/1/19 8 7
22/1/19 9 4
21/1/19 4 12
20/1/19 7 16
.....
so would be possible to use sql /mysql to create another column and calculate average open_price of every 3 days automatically with following display:
date open_price closed_price avg(3)
25/1/19. 6 10 8
24/1/19 10 12 9
23/1/19 8 7 7
22/1/19 9 4 7
21/1/19 4 12
20/1/19 8 16
.....

Why do you need to use an extra field for that?
You can always to calculate that data in your sql query
SELECT *, (open_price+closed_price)/2 as avg_price
FROM `test_price`

Related

MySQL: Get count for each range

There is mysql Ver 8.0.18 value_table as:
value count
1 3
11 1
12 2
22 5
31 1
34 3
35 1
40 3
46 7
What is query to get a total count for each dozen (1-10 - first dozen,11-20 - second , etc..)
as:
1 3
2 3
3 5
4 8
5 7
Query should be flexible, so when some records added to value_table , for example
51 2
62 3
so, it is not necessary to change a query by adding new range (51-60 - 6-th dozen, etc.)
I think you just want division and aggregation:
select min(value), sum(count)
from t
group by floor(value / 10);
To be honest, I'm not sure if the first column should be min(value) or floor(value / 10) + 1.

How to apply a formula for removing data noise in R?

I am working on NGSim Traffic data, having 18 columns and 1180598 rows in a text file. I want to smooth the position data, in the column 'Local Y'. I know there are built-in functions for data smoothing in R but none of them seem to match with the formula I am required to apply. The data in text file looks something like this:
Index VehicleID Total_Frames Local Y
1 2 5 35.381
2 2 5 39.381
3 2 5 43.381
4 2 5 47.38
5 2 5 51.381
6 4 8 504.828
7 4 8 508.325
8 4 8 512.841
9 4 8 516.338
10 4 8 520.854
11 4 8 524.592
12 4 8 528.682
13 4 8 532.901
14 5 7 39.154
15 5 7 43.153
16 5 7 47.154
17 5 7 51.154
18 5 7 55.153
19 5 7 59.154
20 5 7 63.154
The above data columns are just example taken out of original file. Here you can see 3 vehicles, with vehicle IDs = 2, 4 and 5 but in fact there are 2169 vehicles with different IDS. The column Total_Frames tell us how many times vehicle Id of each vehicle is repeated in the first column, for example in the table above, vehicle ID 2 is repeated 5 times, hence '5' in Total_Frames column. Following is the formula I am required to apply to remove data noise (smoothing) from column 'Local Y':
Smoothed Position Value = (1/(Summation of [EXP^-abs(i-k)/delta] from k=i-D to i+D)) * ( (Summation of (Local Y) *[EXP^-abs(i-k)/delta] from k=i-D to i+D))
where,
i = index #
delta = 5
D = 15
I have tried using the built-in functions, which I know of, but they don't smooth the data as required. My question is: Is there any built-in function in R which can do the data smoothing in the way of given formula or which could take this formula as an argument? I need to apply the formula to every value in Local Y which has 15 values before and 15 values after them (i-D and i+D) for same vehicle Id. Can anyone give me any idea how to approach the problem? Thanks in advance.
You can place your formula in a function and then use the apply function of R to apply it to the elements in your "Local Y" column of the dataframe

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.

MySQL query to convert a table into distinct column and other column count array? [duplicate]

This question already has an answer here:
Mysql query to select a distinct column and the count of a value in another column where column like distinct coumn?
(1 answer)
Closed 9 years ago.
My data table is as below:
ID WEEK RESULT
1 13 GOOD
2 13 BAD
3 13 GOOD
4 13 WORST
5 14 GOOD
6 14 BAD
7 14 WORST
8 15 BAD
9 15 WORST
I need a sql query to create an array as below:
WWEK GOOD_RESULT BAD_RESULT WORST_RESULT TOTAL
13 2 1 1 4
14 1 1 1 3
15 0 1 1 2
Can anyone please help me to find an appropriate mysql query?
SELECT
WEEK,
SUM(RESULT='GOOD') As GOOD_RESULT,
SUM(RESULT='BAD') As BAD_RESULT,
SUM(RESULT='WORST') AS WORST_RESULT,
COUNT(*) As TOTAL
FROM YourTable
GROUP BY
WEEK
Please see fiddle here.

SSRS 2008 Break the column in different pages

I am creating a SSRS 2008 Report
in my report i have 200 columns, it is difficult for visualizing purpose,
is there any way in SSRS by which i can show 20 column per page with row hearer in each page.
ex..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 .....................
2010 5 4 8 7 6
2011 9 7 5 8 9 4 2 5
2012 1 2 4 5 3
2013
can i break to show only 20 columns in first page & reamin 20 second page & show on with column heaer repeat in each page
Thankyou
Assuming that your column IDs are sequential, simply set up a new column grouping as =Floor(Fields!columnID.Value/20) above your existing column ID grouping and set a page break on your new grouping.