Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a SUM without group by

Here is my problem..

Actual   Auction   Ammanat   id
7000     500       100       228,229
7000     100       100       228,229
7000     900       100       228,229
5000     0         0         230

I want result as given below

Actual   Auction   Ammanat   Remaining  id
7000     500       100       5550       228,229
7000     100       100       5550       228,229
7000     900       100       5550       228,229
5000     0         0         5000        230

Here, Remaining is (sum(auction)-actual).

I am using PostgreSQL. But if anyone know solution in SQL Server, it will be OK.

like image 214
Aijaz Chauhan Avatar asked Oct 09 '13 12:10

Aijaz Chauhan


People also ask

Does SUM require GROUP BY?

SUM is used with a GROUP BY clause. The aggregate functions summarize the table data. Once the rows are divided into groups, the aggregate functions are applied in order to return just one value per group. It is better to identify each summary row by including the GROUP BY clause in the query resulst.

Can you use aggregate functions without GROUP BY?

GROUP BY in SQL, Explained And data aggregation is impossible without GROUP BY! Therefore, it is important to master GROUP BY to easily perform all types of data transformations and aggregations. In SQL, GROUP BY is used for data aggregation, using aggregate functions.

Can I use having Without GROUP BY clause?

Having can be used without groupby clause,in aggregate function,in that case it behaves like where clause. groupby can be used without having clause with the select statement. 3. The having clause can contain aggregate functions.

Can we use count without GROUP BY in SQL?

Using COUNT, without GROUP BY clause will return a total count of a number of rows present in the table. Adding GROUP BY, we can COUNT total occurrences for each unique value present in the column. we can use the following command to create a database called geeks.


2 Answers

You need a to use a window function - http://www.postgresql.org/docs/9.3/static/tutorial-window.html

Something like:

(Sum(Auction) OVER ()) - actual AS Remaining
like image 147
Idan Arye Avatar answered Oct 05 '22 22:10

Idan Arye


Idan is 100% correct. I want to provide an explanation of this:

(SUM (Auction) OVER ())
  • OVER () creates a window including all rows from the original query.
  • SUM (Auction) is a window function that calculates the sum of Auction.

Here is further explanation:

  • Window is a user-specified set of rows within a query result set.

  • Window Function computes a value (e.g. SUM) over all rows in the window.

  • All rows are in the window because OVER() does not include PARTITION BY. With PARTITION BY the window would include a subset of rows.

From MSDN:

...the OVER clause defines a window or user-specified set of rows within a query result set. A window function then computes a value for each row in the window. You can use the OVER clause with functions to compute aggregated values... if the OVER clause does not include PARTITION BY. This means that the function will be applied to all rows returned by the query.

like image 41
Shaun Luttin Avatar answered Oct 06 '22 00:10

Shaun Luttin