In my line of work, rollup refers to combining/averaging months or quarters. In this case, the rollup is based on INDCODE and PERIOD. I have a table named INDUSTRY that contains data from quarters 1, 2, 3, and 4. Below is a sample of what it looks like.
State area periodyear period indcode firms avgemp
32 000001 2016 01 447125 25 412
32 000001 2016 02 447125 28 427
32 000001 2016 03 447125 29 429
32 000001 2016 04 447125 26 385
First, I am trying to average certain fields but not all. In this case, it would be firms and avgemp. If I was wanting it to look like the desired result below, can I use the rollup function? I want the new averaged data to be in the same table as quarters 1, 2, 3, and 4. Is it better to put the averaged data in a new table?
State area periodyear period indcode firms avgemp
32 000001 2016 01 447125 25 412
32 000001 2016 02 447125 28 427
32 000001 2016 03 447125 29 429
32 000001 2016 04 447125 26 385
32 000001 2016 00 447125 27 413
Related
I have a table that looks something like this:
Agency Year Total PopGroup
01 2017 3467 3C
01 2018 3444 3C
01 2019 3567 3C
02 2017 1000 1C
02 2018 1354 1C
02 2019 1333 1C
03 2017 6784 2C
03 2018 3453 2C
04 2017 3333 2C
If an agency has a row for year 2019, I want to duplicate this row and call it 2020 (basically, an estimate population for 2020). Desired result:
Agency Year Total PopGroup
01 2017 3467 3C
01 2018 3444 3C
01 2019 3567 3C
01 2020 3567 3C
02 2017 1000 1C
02 2018 1354 1C
02 2019 1333 1C
02 2020 1333 1C
03 2017 6784 2C
03 2018 3453 2C
04 2017 3333 2C
I think I should use something like:
INSERT INTO table
WHERE Year = 2019
but I'm a little stuck. What can I try next?
You are on the right track:
INSERT INTO table (Agency, Year, Total, PopGroup)
SELECT Agency, 2020 as Year, Total, PopGroup
FROM table t
WHERE Year = 2019 ;
you can use the following insert to add a new row exactly same as another one but with some modified data.
insert into table (select agency, 2020, total, popgroup from table where year = 2019)
this will insert a new record exactly same as 2019 but with year 2020
I have a Collectionview(iPad) with
flow layout
Scroll direction: horizonlal
3 items at Y and 7 items at X
Possible Device-Orientation is only Landscape.
The normal sorting is like this(one page):
01 04 07 10 13 16 19
02 05 08 11 14 17 20
03 06 09 12 15 18 21
But i need a sorting like this:
act. page............................next page
01 02 03 04 05 06 07 | 22 23 24 25 26 27 28
08 09 10 11 12 13 14 | 29 30 31 32 33 34 35
15 16 17 18 19 20 21 | 36 37 38 39 40
Is it possible to change the item-position with custom-layout?
I tried to sort the data source pagewise like 01, 08, 15 ... but this dont help if jump to an entry middle of such a page and at all i cannot use sections.
I am really learning a lot here. Thanks to all the support. I have another challenge however.
I want to calculate a weighted Index for a group of rows 'Div' within a particular month as shown below:
Wght tMonth tYear Div Indices
37.5 01 2015 01 157.27
2.7 01 2015 01 127.36
58.7 01 2015 01 142.48
DivIndex 146.11
34.9 01 2015 02 133.33
6.7 01 2015 02 136.49
52.4 01 2015 02 131.34
DivIndex 124.43
43.9 02 2015 01 157.18
44.8 02 2015 01 127.42
DivIndex 126.09
58.7 02 2015 03 145.67
7.5 02 2015 03 134.70
6.7 02 2015 03 137.24
DivIndex 104.72
54.0 03 2015 05 160.61
DivIndex 86.73
48.1 03 2015 04 127.49
58.7 03 2015 04 148.62
DivIndex 148.58
I used Excel to compute the 'DivIndex' and that is what I want to come up with in Sql Server 2008R2.
Thanks in advance for any help.
You can do the calculation as:
select tyear, tmonth,
sum(weight * dev_indices) / sum(weight)
from t
group by tyear, tmonth
order by tyear, tmonth;
I would like to return rolling 12 month averages for each month in a resulting dataset but am not sure how I can do this.
I thought the following script would work:
DECLARE #StartDate as datetime
DECLARE #EndDate as datetime
SET #StartDate = '01/04/2011'
SET #EndDate = getdate()
select x.FinYear, x.FinMonth, x.MonthText, avg(TimeSeconds) [AverageTimeSeconds]
from times x
where (x.TimeOfCall >= #StartDate and x.TimeOfCall < #EndDate)
group by x.FinYear, x.FinMonth, x.MonthText
order by x.FinYear, x.FinMonth
but it only returns the monthly averages, how do I get the 12 month average leading up to each of the months between the start and end date.
The resulting dataset I am looking for is as follows:
Fin Year Fin Month Month Text Avg Time Seconds R12M Avg Seconds
2015/16 01 Apr 100 101
2015/16 02 May 95 98
2015/16 03 Jun 103 100
2015/16 04 Jul 110 100
2015/16 05 Aug 100 100
2015/16 06 Sep 90 97
2015/16 07 Oct 93 97
2015/16 08 Nov 98 100
2015/16 09 Dec 80 98
2015/16 10 Jan 88 98
2015/16 11 Feb 100 98
2016/17 12 Mar 115 100
2016/17 01 Apr 105 100
2016/17 02 May 98 100
2016/17 03 Jun 95 98
2016/17 04 Jul 102 98
2016/17 05 Aug 109 99
2016/17 06 Sep 104 100
2016/17 07 Oct 98 98
2016/17 08 Nov 99 97
2016/17 09 Dec 90 97
the rolling 12 month average is not an average of the monthly averages but an average of the 12 months leading up to the month in question. So January 2017 would be the average of 01 February 2016 - 31 January 2017 and October 2016 would be 01 November 2015 to 31 October 2016.
I hope you can help :-) .
If you have data for every month, then the following calculates the average over the preceding 12 months (note this is the overall average, not the average of the monthly averages):
select x.FinYear, x.FinMonth, x.MonthText, avg(TimeSeconds)as [AverageTimeSeconds],
(sum(sum(TimeSeconds)) over (order by x.FinYear, x.FinMonth rows between 11 preceding and current row) /
sum(count(*)) over (order by x.FinYear, x.FinMonth rows between 11 preceding and current row)
) as avg_12month
from times x
where x.TimeOfCall >= #StartDate and x.TimeOfCall < #EndDate
group by x.FinYear, x.FinMonth, x.MonthText
order by x.FinYear, x.FinMonth;
Note: The where clause affects the 12-month look-back period. In other worse, the look-back will not include months before this period.
I have a table Test with AcctNbr, Balance and Date columns. This table contains data as below
Acctnbr Balance LastUpdatedDt
123 100 May 08
456 200 May 08
222 300 May 07
333 400 May 07
123 50 May 06
222 120 May 05
678 70 May 04
Am trying to achieve daily balance as below
AcctNbr Balance LastUpdatedDt
123 100 May 08
456 200 May 08
222 300 May 08
333 400 May 08
123 50 May 08
678 70 May 08
123 50 May 07
456 0 May 07
222 300 May 07
333 400 May 07
123 50 May 07
678 70 May 07
123 50 May 06
456 0 May 06
222 120 May 06
333 0 May 06
123 50 May 06
678 70 May 06
123 0 May 05
456 0 May 05
222 120 May 05
333 0 May 05
123 0 May 05
678 70 May 05
123 0 May 04
456 0 May 04
222 0 May 04
333 0 May 04
123 0 May 04
678 70 May 04
Here is my query
SELECT
A.Accountnbr,
dt AS CalendarDate,
A.Balance
FROM
dbo.GetDates('20150504',GETDATE())
cross APPLY
(
SELECT TOP 1 Accountnbr,Balance
FROM dbo.Test
WHERE LastUpdateDt <= dt
ORDER BY LastUpdateDt DESC
) A
This is working fine when i use TOP and Order BY inside CROSS APPLY. But i want to get balances for all accounts. I cannot use TOP inside CROSS APPLY as Test table will be getting updated every day with new accounts, but i need Order By to get correct balances from To and End dates in GetDates function. My question is how to use Order by without using TOP in the above query. Any help is appreciated.
I would use a cte with a row number and partition on account number and date (assuming you want the latest by account and day).
You can then join any tables onto the cte table if you need to.
with balance_cte as (
SELECT Accountnbr,
LastUpdatedDt,
Balance,
row_number() over (partition by Accountnbr. mydate order by LastUpdateDt DESC) as rn
FROM dbo.Test
)
select Accountnbr,
LastUpdatedDt,
Balance
from join balance_cte
where rn = 1