{"slug":"portfolio-analysis-from-the-free-sql-api","qid":"position_weights","label":"Position weights from last close times share count","post_title":"Portfolio Analysis in SQL: Weights to Drawdown","post_url":"/blog/portfolio-analysis-from-the-free-sql-api#q-position_weights","columns":["ticker","shares","last_close","position_value","weight_pct","priced_through"],"rows":[{"ticker":"AAPL","shares":120,"last_close":336.17,"position_value":40341,"weight_pct":25.71,"priced_through":"Oct 7, 2026"},{"ticker":"KO","shares":300,"last_close":86.57,"position_value":25971,"weight_pct":16.55,"priced_through":"Oct 7, 2026"},{"ticker":"XOM","shares":150,"last_close":165.88,"position_value":24881,"weight_pct":15.86,"priced_through":"Oct 7, 2026"},{"ticker":"MSFT","shares":45,"last_close":525.69,"position_value":23656,"weight_pct":15.08,"priced_through":"Oct 7, 2026"},{"ticker":"JNJ","shares":90,"last_close":255.89,"position_value":23030,"weight_pct":14.68,"priced_through":"Oct 7, 2026"},{"ticker":"NVDA","shares":80,"last_close":237.61,"position_value":19009,"weight_pct":12.12,"priced_through":"Oct 7, 2026"}],"shape":"table","sql":"WITH holdings AS\n(\n    SELECT 'AAPL' AS ticker, 120 AS shares\n    UNION ALL SELECT 'MSFT', 45\n    UNION ALL SELECT 'NVDA', 80\n    UNION ALL SELECT 'KO',   300\n    UNION ALL SELECT 'JNJ',  90\n    UNION ALL SELECT 'XOM',  150\n),\nmarks AS\n(\n    SELECT\n        ticker,\n        argMax(toFloat64(close), (date, _ingest_time)) AS last_close,\n        max(date)                                      AS last_session\n    FROM global_markets.stocks_daily_aggs\n    WHERE ticker IN ('AAPL', 'MSFT', 'NVDA', 'KO', 'JNJ', 'XOM')\n      AND date >= today() - 30\n    GROUP BY ticker\n),\npositions AS\n(\n    SELECT\n        h.ticker                          AS ticker,\n        h.shares                          AS shares,\n        round(m.last_close, 2)            AS last_close,\n        round(h.shares * m.last_close, 0) AS position_value,\n        m.last_session                    AS last_session\n    FROM holdings AS h\n    INNER JOIN marks AS m ON m.ticker = h.ticker\n)\nSELECT\n    ticker,\n    shares,\n    last_close,\n    position_value,\n    round(100 * position_value / sum(position_value) OVER (), 2) AS weight_pct,\n    formatDateTime(last_session, '%b %e, %Y')                    AS priced_through\nFROM positions\nORDER BY weight_pct DESC","computed_at":"2026-10-07T15:11:29.708560+00:00","elapsed":0.136766515}