Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 30-Day Asset Average

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Rolling 30-Day Asset Average. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

BlackRock's Aladdin risk analytics workflow needs a time-series view of recent asset performance. Given daily closing prices and an in-scope asset set, write a PostgreSQL query that calculates each asset's daily return and rolling 30-calendar-day average return.

Requirements

  1. Restrict the analysis to assets in portfolio BLK_CORE where is_in_scope = TRUE.
  2. Calculate each daily return as the current closing price divided by the previous available closing price minus one, using LAG partitioned by asset.
  3. Calculate the rolling average over the current date and preceding 29 calendar days, not merely the preceding 30 rows.
  4. Preserve the first price observation for each asset, returning NULL for its daily return and rolling average.
  5. Return results ordered by ticker and price date, with returns rounded to six decimal places.

Schema

assets
ColumnTypeDescription
asset_idPKINTUnique asset identifier
tickerVARCHAR(12)Trading symbol
asset_nameVARCHAR(100)Asset name
portfolio_codeVARCHAR(30)Portfolio membership code
is_in_scopeBOOLEANWhether the asset is included in the analysis
asset_prices
ColumnTypeDescription
price_idPKINTUnique price observation identifier
asset_idINTReferences assets.asset_id
price_dateDATEClosing-price date
close_priceNUMERIC(12,4)Closing price
Tablesassetsasset_prices
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results