SQL - SUM and grouping - sql

I have a following table:
ITEM
Date
VALUE
START DATE
END DATE
1
01 Jan 2023
15
01 Jan 2023
02 Jan 2023
1
02 Jan 2023
20
02 Jan 2023
03 Jan 2023
1
03 Jan 2023
25
03 Jan 2023
04 Jan 2023
1
04 Jan 2023
40
04 Jan 2023
05 Jan 2023
2
01 Jan 2023
30
01 Jan 2023
02 Jan 2023
2
02 Jan 2023
20
02 Jan 2023
03 Jan 2023
2
03 Jan 2023
10
03 Jan 2023
04 Jan 2023
2
04 Jan 2023
40
04 Jan 2023
05 Jan 2023
From here I need to have calculated sum of all values for every given row/date that are within dates in Start and End Date columns (boundaries included), so it is grouped by item and per date.
ITEM
Date
VALUE_SUM
1
01 Jan 2023
35
1
02 Jan 2023
45
1
03 Jan 2023
65
1
04 Jan 2023
40
2
01 Jan 2023
50
2
02 Jan 2023
30
2
03 Jan 2023
50
2
04 Jan 2023
40
Thanks for your help!

A simple way is to use a correlated subquery to do the calculation:
select item, date,
(select sum(value) from table t2
where t2.item = t1.item
and t2.date between t1.start_date and t1.end_date)
from table t1

Assuming you are only working with one month of data - one option could be to extract the day value from each date and then sum up each value while grouping by day.
This can be done by firstly creating a new variable in your dataset to store the day value:
alter table dataset add day int;
Values can then be extracted:
update dataset set day=extract(day from date);
Then, it is a matter of grouping the values by day:
select day, sum(value) as value_sum from dataset group by day order by day;

Related

Indexing Time in Redshift

i have a dataset with that has sales by month and a column for when an promotion happens for a number of products.
i want to index the months so that i can compare the different products over a pre and post 12m period easily
this is the data i have
Date
sales.
Promo?
Jan 21
100
Feb 21
110.
Mar 21
100.
apr 21
110.
may 21
90.
jun 21
100.
jul 21
120.
aug 21
140.
X
sep 21
100.
oct 21
90.
nov 21
100.
dec 21
120.
jan 22
110.
feb 22
100
this is what i want
can someone tell me how to do this in redshift?
Date
sales.
Promo?
month index
Jan 21
100
-7.
Feb 21
110.
-6
Mar 21
100.
-5
apr 21
110.
-4
may 21
90.
-3
jun 21
100.
-2
jul 21
120.
-1
aug 21
140.
X
0
sep 21
100.
1
oct 21
90.
2
nov 21
100.
3
dec 21
120.
4
jan 22
110.
5
feb 22
100
6

Making a duplicate row in SQL table based on condition, and replacing a value in the new row

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

How do I calculate group subtotals for a specific number of rows in a table

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;

How to get the data in decline pattern/trending using Oracle SQL?

How can I get the result only for those monthly data in decline pattern/trending ?
And the data as below;
ID JAN FEB MAR APR MAY JUN
112 50 45 40 35 30 20
113 30 30 30 30 30 30
114 20 25 20 20 20 20
115 45 50 60 60 30 30
Expected output ;
ID JAN FEB MAR APR MAY JUN
112 50 45 40 35 30 20
115 45 50 60 60 30 30
Thanks !
SELECT *
FROM your_table
WHERE JAN > JUN
or
SELECT *
FROM your_table
WHERE JAN > FEB
AND FEB > MAR
AND MAR > APR
AND APR > MAY
AND MAY > JUN
It appears that what you're looking for is
SELECT *
FROM SOME_TABLE
WHERE JAN > FEB OR
FEB > MAR OR
MAR > APR OR
APR > MAY OR
MAY > JUN
Best of luck.

T-SQL - Rolling 12 Month Average

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.