Using alias in subquery
问题 I'm running the following query to get the open positions on a portfolio: SELECT SUM(trades.quantity) as total_quantity, SUM(trades.price) as total_cost, SUM(trades.price)/SUM(trades.quantity) as cost_per_share, trades.ticker, tickers.code FROM (trades) LEFT JOIN tickers ON trades.ticker = tickers.id GROUP BY tickers.code HAVING total_quantity > 0 ORDER BY tickers.code I'd like to add an extra column to show the weightening of a position, i.e.: total_cost/SUM(total_cost) -- Dividing any given