Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

SQL to get last 4 quarter sum total

$
0
0

Hi Guys

I've got some SQL skill but this one has me thrown. I have a FACT table with Partner_Key, QuarterName and Total_Assets. Finance_Id is the primary key:

CREATE TABLE [dbo].[Fact_FINANCE](
    [Finance_ID] [int] NOT NULL,
    [Partner_Key] [int] NOT NULL,
    [QuarterName] [varchar](6) NOT NULL,
    [Total_Assets] [decimal](15, 2) NOT NULL

)

The data looks as follows:

  • 1,9,2015Q1,1500
  • 2,9,2015Q2,1000
  • 3,9,2015Q3,2500
  • 4,9,2015Q4,900
  • 5,9,2016Q1,2400
  • 6,9,2016Q2,1600
  • 7,9,2016Q3,1000
  • 8,9,2016Q4,1200

I would like a query that returns the current quarter with the sum of the current and previous 3 quarters in one line:

  • Partner_Key: 9
  • QuarterName: 2016Q4
  • Sum(Total_Assets): 1200 + 1000 + 1600 + 2400 = 6200

The total for 2016Q3 would then be 1000 + 1600 + 2400 + 900 = 5900

Any advice for me?

Thanks!




Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>