Skip to content
Advertisement

Tag: sum

SQL sum grouped by field with all rows

I have this table: I want this result: I tried this: But get the error that subquery returns different number of rows. How can I do it? I want all the rows of sale_lines table selecting all fields and adding the sum(price) grouped by sale_id. Answer You can use window function : If you want sub-query then you need to

Multiple SUM in one query with multiple where

I’m trying to write a SQL query that produces a table with summarized values for each year and month. I have a table that looks something like this: And another table that looks like this: I want to have a query that returns the sum for each month like this: Explanation of the output: Year and Month are obvious RegTime

Cumulative Sum When rows have the same value sql sql server

I’d like to do cumulative sum of the column SubTotal to get a cumulative RunningTotal column. But I noticed that if rows have the same values, the cumulative sum doesn’t work. e.g. In the below example, the row 5 and row 6 have the same SubTotal value of 838.9178. Because of that, the RunningTotal column doesn’t sum up for row

Advertisement