Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

SQL to sum last 12 month values from same table

$
0
0
What's the quickest way to sum "last 12 months" values from one date column and update 2nd column?e.g. original data contains "Date" and "Value", and we need to update new field "Date2" with the sum of "last 12 months" values.so goal would be (very simple example)Date Value Date2------------- ------ --------01/08/2016 100 120001/07/2016 100 120001/06/2016 100 115001/05/2016 10001/04/2016 10001/03/2016 10001/02/2016 10001/01/2016 10001/12/2015 10001/11/2015 10001/10/2015 10001/09/2015 10001/08/2015 10001/07/2015 50I've only populated first 3 rows of Date2 as the source data only goes back to 01/07/2015 in this example.My thoughts were to use row(over partition ...), and join back to same table.

Viewing all articles
Browse latest Browse all 3145

Trending Articles