- How do I calculate a percentage of the total in SQL?
- Ask in plain English — 'each product's revenue as a percent of total.' nlqdb compiles the window aggregate
100.0 * revenue / SUM(revenue) OVER (), runs it, and returns each row's share plus the SQL. The OVER () broadcasts the grand total onto every row so you divide without a self-join. The honest limit: the 100.0 * (or a cast) is what stops integer division flooring the share to 0.
- Why does my percentage come out as 0 in SQL?
- Integer division. If both the value and the total are integer columns,
revenue / SUM(revenue) OVER () computes an integer quotient — almost always 0 — before any percentage. Multiply by 100.0 (or cast one side to numeric) so the division happens in floating point. nlqdb compiles the 100.0 * form and shows it in the SQL so you can confirm the share isn't silently floored.
- What's the difference between percent of total and percent of a group?
- The denominator. Percent of the grand total uses an empty window,
SUM(x) OVER (); percent within each group uses SUM(x) OVER (PARTITION BY region), so each region's rows sum to 100%. They answer different questions from the same rows. nlqdb picks one from how you phrase it and shows the OVER (...) clause in the SQL so you can confirm the denominator.
- Can I calculate percentage of total on a Postgres database I already run?
- Yes — connect it with the signed-in BYO connect verb (
nlq db connect; see /solve/query-existing-postgres-in-natural-language) and ask for the share breakdown in place, no ETL into a separate store. The honest limits: BYO connect is signed-in only (not the public embed), and nlqdb returns the shares with a read-only query — it doesn't persist a materialized breakdown for you.