Portfolio Analysis in SQL: Weights to Drawdown
Position weights from last close times share counttable ·
2026-10-07 · 6×6
Pairwise daily return correlation, trailing yearranking ·
2026-10-07 · 15×2
Weekly peak-to-trough drawdown of the blended portfolioseries ·
2026-10-07 · 53×4
Trailing twelve-month dividend income by holdingtable ·
2026-10-07 · 6×6
Cumulative weight and the Herfindahl concentration indexranking ·
2026-10-07 · 6×4
RSP vs SPY: Equal Weight S&P 500
Cap weight versus equal weight across a ten name basketseries ·
2026-10-04 · 10×5
RSP share volume: each quarter's heaviest session against its medianseries ·
2026-10-04 · 11×6
RSP and SPY price paths, both indexed to 100 in January 2019series ·
2026-10-04 · 93×5
Calendar year price change, RSP against SPY, first close to last closeranking ·
2026-10-04 · 15×4
Position weights from last close times share count
Position weights from last close times share count
| ticker | shares | last_close | position_value | weight_pct | priced_through |
|---|---|---|---|---|---|
| AAPL | 120 | 336.17 | 40341 | 25.71 | Oct 7, 2026 |
| KO | 300 | 86.57 | 25971 | 16.55 | Oct 7, 2026 |
| XOM | 150 | 165.88 | 24881 | 15.86 | Oct 7, 2026 |
| MSFT | 45 | 525.69 | 23656 | 15.08 | Oct 7, 2026 |
| JNJ | 90 | 255.89 | 23030 | 14.68 | Oct 7, 2026 |
| NVDA | 80 | 237.61 | 19009 | 12.12 | Oct 7, 2026 |
the exact SQL behind every number
WITH holdings AS
(
SELECT 'AAPL' AS ticker, 120 AS shares
UNION ALL SELECT 'MSFT', 45
UNION ALL SELECT 'NVDA', 80
UNION ALL SELECT 'KO', 300
UNION ALL SELECT 'JNJ', 90
UNION ALL SELECT 'XOM', 150
),
marks AS
(
SELECT
ticker,
argMax(toFloat64(close), (date, _ingest_time)) AS last_close,
max(date) AS last_session
FROM global_markets.stocks_daily_aggs
WHERE ticker IN ('AAPL', 'MSFT', 'NVDA', 'KO', 'JNJ', 'XOM')
AND date >= today() - 30
GROUP BY ticker
),
positions AS
(
SELECT
h.ticker AS ticker,
h.shares AS shares,
round(m.last_close, 2) AS last_close,
round(h.shares * m.last_close, 0) AS position_value,
m.last_session AS last_session
FROM holdings AS h
INNER JOIN marks AS m ON m.ticker = h.ticker
)
SELECT
ticker,
shares,
last_close,
position_value,
round(100 * position_value / sum(position_value) OVER (), 2) AS weight_pct,
formatDateTime(last_session, '%b %e, %Y') AS priced_through
FROM positions
ORDER BY weight_pct DESC
More from this analysisPortfolio Analysis in SQL: Weights to Drawdown
Trailing twelve-month dividend income by holding
table 6×6
→
Weekly peak-to-trough drawdown of the blended portfolio
series 53×4
→
Pairwise daily return correlation, trailing year
ranking 15×2
→
Cumulative weight and the Herfindahl concentration index
ranking 6×4
→
See all 3,171 queries →