Using SQL Server, I am looking for a way to calculate monthly averages using the number of completed months to date in my dividend in a division formula. So, looking for a way to solve for “x” in the following equation:
Formula:
Number of Units) / x = monthly average
Example Data:
Jan # of units = 20
...Monthly average = 20
Feb # of units = 40
...Monthly average is (20+40) / 2 (months) = 30
Mar # of units = 10
...Monthly average is (20+40+10) /3 (months) = 23
Results Table:
Year Monthly Average
2022 23
Just to expand on my comment
Declare #YourTable Table ([Period] varchar(50),[Units] int)
Insert Into #YourTable Values
('2022-01-01',20)
,('2022-02-01',40)
,('2022-03-01',10)
Select *
,AvgUnits = avg(Units) over (partition by year(period) order by period)
From #YourTable
Results
Period Units AvgUnits
2022-01-01 20 20
2022-02-01 40 30
2022-03-01 10 23
Related
I have a table populated with timesheet information that looks like the following:
Interval
Team
Count
9:30AM
Team1
0
9:00AM
Team1
2
11:00AM
Team3
2.5
12:30AM
Team2
4
.
.
.
This table represents how many employess (Count, a float) are working for which team (Team, as Str) during which time of day (Interval, datatype Time).
I have tried the below:
SELECT *
FROM
(SELECT
[Interval] AS 'TimeBlock'
,Team
,ROUND([Count]/2,1) AS 'Hours'
FROM MyTable
WHERE [Interval] >= CONVERT(TIME, DATEADD(MINUTE, ROUND(DATEDIFF(MINUTE, 0, GETDATE()) / 30.0, 0) * 30, 0))
) AS "SUB"
PIVOT
(SUM(Hours)
FOR Team IN
([Team1]
,[Team2]
,[Team3])
) AS "Pivot";
Which yields a pivot table that lists the team across the top (along the pivot, or has the teams as the columns). What I would like is to show the time intervals across the top, and the team listed down the left hand side.
I know that listing the time intervals in the pivot will give me my desired result. However, I do not want to list every 30 min time interval between 6:00 AM and Midnight
As a work around I have simply been using excel to unpivot the columns, and then pivot along the time interval.
BONUS: Have a total by team as the final column and total by time as the final row
Ex//
Team
9:30 AM
10:00 AM
10:30 AM
11:00 AM
TOTAL
Team1
0
1
0.5
1
2.5
Team2
2
3
1
3
9
Team3
2.5
1
2
2
7.5
TOTAL
4.5
5
3.5
6
19
I am trying to subtract the row values from a column in SSRS report builder to calculate the per day cumulative hourly run hour having first row value as lifetime run hour till yesterday. Could anyone please help me how to proceed. Please refer the image for required report format.
DATE TIME RUNHR COLUMN A(RUN HR in MINUTES) ROW NO
12/12/2018 00:00AM 100 100(TOTAL RUN HR TILL YEST MID-NIGHT11:45PM) 1
12/12/2018 00:30AM 101 1 (I HAVE TO START SUBTRACTING FROM HERE) 2
12/12/2018 01:00AM 105 5 3
---------------------------------------------------------------------------
12/12/2018 11:00PM 130 30 47
12/12/2018 11:30PM 135 35 48
I want to display the value as shown in COLUMN A, where the first row in COLUMN A shows the total RUNHR from yesterday and subsequent rows (2 to 48) showing the cumulative (to that time) RUNHR for that day only (i.e. since the value in the first row).
Given RUNHR is cumulative, you could do this by joining on the minimum value of RUNHR for that period (assuming your dataset is based in Sql):
SELECT PumpId, PumpName, SampleTime, RUNHR, StartingValue, (RUNHR - StartingValue) AS COLUMNA
FROM PumpDataTable A
INNER JOIN
(SELECT PumpId, MIN(RUNHR) AS StartingValue
FROM PumpDataTable
WHERE SampleTime >= #StartTIme AND SampleTime <= #EndTime GROUP BY PumpId) B ON A.PumpId = B.PumpId
WHERE PumpId = #PumpId
AND SampleTime >= #StartTIme AND SampleTime <= #EndTime
ORDER BY SampleTime
I am trying to replicate this behavior in SQL Server 2014. I have total dollars being worked per month, but I want a new column that takes the dollars and sums it by the current and next 2 months and places the total in the current month row. How can I accomplish this with SQL server?
Date Dollars 3 MONTH SUM
1/1/2018 10 37
2/1/2018 12 32
3/1/2018 15 36
4/1/2018 5 36
5/1/2018 16 34
6/1/2018 15 23
7/1/2018 3 27
8/1/2018 5 40
9/1/2018 19 46
10/1/2018 16 27
11/1/2018 11 11
I tried:
SUM(dollars) OVER (ORDER BY date ROWS BETWEEN current row and 2 following) AS [3 month sum]
but the calculation wasn't accurate. Is this the proper way to do it and I am just not doing it correctly or is there a better method?
This seems to work, and look very simple to read
declare #Incomings table([Date] Datetime, Dollars int)
insert into #Incomings
values
('20180101', 10)
,('20180201', 12)
,('20180301', 15)
,('20180401', 5)
,('20180501', 16)
,('20180601', 15)
,('20180701', 3)
,('20180801', 5)
,('20180901', 19)
,('20181001', 16)
,('20181101', 11)
select * , (select SUM(Dollars) from #Incomings where [Date] between i.[Date] and DATEADD(MONTH,2,i.[Date]))
from #Incomings i
I have a subquery with avg reservoir inflow pr day of year (from 1-365). Now I would like to calculate a smoothed/moving average for each day of year in a new column.
Example: for january 1st (DayOfYear = 1) I would like to calculate a smoothed average of 21 days (10 pre and 10 post days). I.e an avg of days ranging from (356-11). For day of year 55 the avg should be calculated on days of the year ranging from (45-65).
Her is the unfinished query based on a subquery called 'sub' where the 10 years of inflow first are averaged on day of year;
DECLARE #Dager int ;
SET #Dager = 10; /* # days pre and post the actual day of year to be included in avg */
Select sub.Magasin, sub.DayOfYear, AVG(sub.Inflow) as AvgInflow
FROM (SELECT Date, Magasin, Datepart(dy,Date) as DayOfYear, Value as Inflow
FROM inputtable
WHERE Date >= DATEFROMPARTS(2008,1,1) and Date <= DATEFROMPARTS(2017,12,31)) sub
GROUP By sub.Magasin, sub.DayOfYear
ORDER BY sub.magasin, sub.DayOfYear
Without any sample data, I'm going to suggest this for SQL Server 2012+
(Your SQL looks like SQL Server 2012+)
SELECT
Magasin,
Datepart(dy,Date) AS DayOfYear,
AVG(Inflow) OVER (
PARTITION BY Magasin
ORDER BY YEAR(Date), Datepart(dy,Date)
ROWS BETWEEN 10 PRECEDING AND 10 FOLLOWING)
FROM
inputtable
WHERE
Date >= DATEFROMPARTS(2008,1,1) and Date <= DATEFROMPARTS(2017,12,31))
This is my table.
Assume the current month is June. Previous month is May.If balance amount exists for both current and previous months then populate the balance amount for current month. If balance amount does not exist for current month then populate the value for previous month.
Input table.
Transaction_Number Status_Indicator Balance Amount Month
20 Y 200 June
20 Y 500 May
21 Y 600 June
21 Y 700 May
22 Y 800 June
23 Y 900 May
Output:
Transaction_Number Balance_Amount
20 200
21 600
22 800
23 900
Appreciate any help on this!
Note: This is not a tested code... right now I don't have the control center.
select currentmonth.Transaction_Number,(currentmonth.data1+previousmonth.data2)
from
(select Transaction_Number, sum(Balance Amount) data2
from <<table name>>
where month=MONTH(current date)-1
group by Transaction_Number) previousmonth
Left join
(select Transaction_Number, sum(Balance Amount) data1
from <<table name>>
where month=MONTH(current date)
group by Transaction_Number) currentmonth
on previousmonth.Transaction_Number = currentmonth.Transaction_Number
group by Transaction_Number
Let me know how it goes