Calculate Moving Average - MS ACCESS - ms-access

I would like to calculate the moving average of 4 rows. I have searched online and tried different methods but I have not got the result. This is an example of my table
Week DATES CURRYR MOVINGAVG
1 4/7/2017 351,929.76
2 4/14/2017 352,202.99
3 4/21/2017 483,987.60
4 4/28/2017 358,125.55 (AVG CURRYR 0F #1-#4)
5 5/5/2017 335,180.47 (AVG CURRYR 0F #2-#5)
6 5/12/2017 421,693.56 (AVG CURRYR 0F #3-#6)
7 5/19/2017 394,410.34 (AVG CURRYR 0F #4-#7)
8 5/26/2017 437,064.82 (AVG CURRYR 0F #5-#8)
9 6/2/2017 536,921.87 (AVG CURRYR 0F #6-#9)
10 6/9/2017 358,177.15 (AVG CURRYR 0F #7-#10)
11 6/16/2017 418,651.36 (AVG CURRYR 0F #8-#11)
12 6/23/2017 402,590.79 (AVG CURRYR 0F #9-#12)
13 6/30/2017 407,775.24 (AVG CURRYR 0F #10-#13)
14 7/7/2017 391,020.15 (AVG CURRYR 0F #11-#14)
And so no....
Is there a way I could execute this in MS ACCESS

I asusme your real data have a few more things to join on in which case you'd need to bring them into the sub query. Also this only works because the Weeks are a complete list (i.e. you don't have week 15, week 16, week 18, week 20, that's going to mess it up and the logic would need to be changed).
SELECT o.Week
,o.DATES
,o.CURRYR
,(
SELECT avg(CurrYR)
FROM Table1 i
WHERE i.week <= o.week
AND i.week >= (o.week - 3)
having count(*) >=4
) AS MovingAverage
FROM Table1 o
.
Week DATES CURRYR MovingAverage
1 4/7/2017 351,929.76
2 4/14/2017 352,202.99
3 4/21/2017 483,987.60
4 4/28/2017 358,125.55 386561.475
5 5/5/2017 335,180.47 382374.1525
6 5/12/2017 421,693.56 399746.795
7 5/19/2017 394,410.34 377352.48
8 5/26/2017 437,064.82 397087.2975
9 6/2/2017 536,921.87 447522.6475
10 6/9/2017 358,177.15 431643.545
11 6/16/2017 418,651.36 437703.8
12 6/23/2017 402,590.79 429085.2925
13 6/30/2017 407,775.24 396798.635
14 7/7/2017 391,020.15 405009.385

Related

Fetch data between morning and evening

im trying to fetch the data between two times, i.e., morning (7:30AM to 7:30PM) evening (7:30PM to 7:30AM).
required only hour based output.{ > 7:30 < 19:30 as morning and > 19:30 < 7:30 as night)
Please suggest the query
Tablename: data
----------------
created_date services_name id
28-08-2022 18:54 KANE 1
28-08-2022 19:00 BAPLO 2
28-08-2022 23:22 BAPLO 3
28-08-2022 23:40 VLOTLS 4
29-08-2022 00:02 DELLP 5
29-08-2022 00:42 SECON 6
29-08-2022 02:00 BAPLO 7
29-08-2022 03:00 PRODC 8
29-08-2022 05:14 DELLP 9
29-08-2022 05:30 SECON 10
29-08-2022 05:42 SECON 11
im using below command to fetch output
SELECT
CONCAT( HOUR(created_date), ' to ', CONCAT( HOUR(created_date), ':59:59' ) ) as time_frame,
COUNT(*)
FROM
data
GROUP BY
DATE(created_date),
HOUR(created_date)
ORDER BY
DATE(created_date),
HOUR(created_date)
Out as below
0 to 0:59:59 2
2 to 2:59:59 1
3 to 3:59:59 1
5 to 5:59:59 3
18 to 18:59:59 1
19 to 19:59:59 1
23 to 23:59:59 2
But i required as
morning ( between 07:30 to 19:30) count is 2
evening ( between 19:30 to 07:30) count is 9
You can perform a UNION to get two separate counts, one for morning times and one evening times. Within the WHERE clause, you can convert created_date to a time format using the TIME() data type and then plug in your morning and evening times within the BETWEEN condition.
Note that you have to use the NOT operator to negate the BETWEEN operator for the evening count because BETWEEN works within a range (small to large 0..1, not large to small 1..0) so the NOT operator will essentially get everything else outside of the morning BETWEEN range.
SELECT 'Morning' as time_frame, COUNT(*) AS `count` FROM temp
WHERE TIME(created_date)
BETWEEN '07:30:00' AND '19:30:00'
UNION
SELECT 'Evening' as time_frame, COUNT(*) AS `count` FROM temp
WHERE TIME(created_date)
NOT BETWEEN '07:30:00' AND '19:30:00'
Output:
time_frame
count
Morning
2
Evening
9
See Fiddle.

MySQL - How to DISREGARD the records less than the CURRENT_DATE with the same FOREIGN KEY?

This is might be a simple problem for some but I badly need help.
Here is my table namely contribution:
id
entityno
oreceipt
oramount
ordate
datestarted
validity
5
8
1
60
2021-01-04
2021-01-04
2021-02-04
6
9
2
60
2021-01-04
2021-01-04
2021-02-04
7
8
3
60
2020-12-04
2020-12-04
2021-01-04
11
8
4
60
2021-02-22
2021-03-04
2021-03-04
enter image description here
What query can I use to select the records less than the current_date using validity as the basis?
The expected output is the record on the red marker corresponds to entityno = 9 only. Since entityno = 9 was expired last 04 February 2021 and entityno = 8 will expire on 04 March 2021.
Expected output:
id
entityno
oreceipt
oramount
ordate
datestarted
validity
6
9
2
60
2021-01-04
2021-01-04
2021-02-04
Fiddle
Any help is highly appreciated. Thanks!
You probably can do something like this:
SELECT A.* FROM contribution A
JOIN
(SELECT entityno, MAX(validity) mxv
FROM contribution
GROUP BY entityno) B
ON A.entityno=B.entityno AND A.validity=B.mxv
WHERE validity < CURDATE();
Idea is to:
Get the entityno with their maximum validity date, hence MAX(validity).
Then make it as a sub-query and JOIN with contribution table by matching entityno and validity=MAX(validity). This suppose to return each entityno with their maximum validity.
Then with that do a WHERE condition to get any entityno that have validity date smaller than CURDATE().
Demo fiddle

i am having a matrix in ssrs with 12 month columns and category with date as below.How to find the difference between first row and second row

Category/Date jul aug sep oct nov dec ...jan
AA on 2020jun 5 6 3 8 2 7 ... 4
AA on 2020May 7 3 2 6 5 5 ....7
Difference -2 1 1 2 -3 2 ...-3
I am using wcf query to fetch the data.The dataset looks like
category -AA
Date - 2020Jun
Month1 4
Month12 7
Month11 2
Month10 8
i am using a switch statement like below to display each column value
switch(Reportdate.Month(jun 2020) = 1, Fields.Month1.Value,
Reportdate.Month = 12, Fields.Month12.Value)
The problem i am facing is with finding the difference in each column

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.

Get the hits of those days which have more hits than previous day

How to Get the hits of those days which have more hits than previous day.
Table structure
Date Hits
1-Mar-2013 36
2-Mar-2013 2
3-Mar-2013 99
4-Mar-2013 82
5-Mar-2013 34
6-Mar-2013 36
7-Mar-2013 56
8-Mar-2013 81
9-Mar-2013 8
10-Mar-2013 99
11-Mar-2013 12
12-Mar-2013 76
13-Mar-2013 75
14-Mar-2013 80
15-Mar-2013 69
16-Mar-2013 12
17-Mar-2013 3
18-Mar-2013 75
19-Mar-2013 7
20-Mar-2013 54
21-Mar-2013 82
22-Mar-2013 50
23-Mar-2013 29
24-Mar-2013 17
25-Mar-2013 78
26-Mar-2013 97
27-Mar-2013 76
28-Mar-2013 57
29-Mar-2013 28
30-Mar-2013 17
Can somebody suggest me a mysql queryy for doing this in one query itself. The Date column tells the date and the hits colum gives the hits on that day.
Try something like this:
SELECT t2.*, t1.hits AS Previous_day_hits
FROM tab1 t1
INNER JOIN tab1 t2 ON t2.DATE = date_add(t1.DATE, INTERVAL 1 DAY)
AND t2.hits > t1.hits;
sqlfiddle demo
I added the column with Previous_day_hits for easier validation.
It's called a self join. Basically you get two copies of the table and work out a rule to link them