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.
我正在使用PostgreSQL。但是如果有人知道SQL Server中的解决方案,那就没问题了。
2 个解决方案
#1
12
You need a to use a window function - http://www.postgresql.org/docs/9.3/static/tutorial-window.html
你需要一个使用窗口函数 - http://www.postgresql.org/docs/9.3/static/tutorial-window.html
Something like:
就像是:
(Sum(Auction) OVER ()) - actual AS Remaining
#2
5
Idan is 100% correct. I want to provide an explanation of this:
Idan 100%正确。我想提供一个解释:
(SUM (Auction) OVER ())
-
OVER ()
creates a window including all rows from the original query. - OVER()创建一个窗口,其中包含原始查询中的所有行。
-
SUM (Auction)
is a window function that calculates the sum ofAuction
. - SUM(拍卖)是一个窗口函数,用于计算拍卖总和。
Here is further explanation:
这是进一步的解释:
-
Window is a user-specified set of rows within a query result set.
Window是查询结果集中用户指定的一组行。
-
Window Function computes a value (e.g.
SUM
) over all rows in the window.窗口函数计算窗口中所有行的值(例如SUM)。
-
All rows are in the window because
OVER()
does not includePARTITION BY
. WithPARTITION BY
the window would include a subset of rows.所有行都在窗口中,因为OVER()不包括PARTITION BY。使用PARTITION BY窗口将包含行的子集。
来自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.
... OVER子句定义查询结果集中的窗口或用户指定的行集。然后,窗口函数计算窗口中每行的值。您可以将OVER子句与函数一起使用来计算聚合值...如果OVER子句不包含PARTITION BY。这意味着该函数将应用于查询返回的所有行。
#1
12
You need a to use a window function - http://www.postgresql.org/docs/9.3/static/tutorial-window.html
你需要一个使用窗口函数 - http://www.postgresql.org/docs/9.3/static/tutorial-window.html
Something like:
就像是:
(Sum(Auction) OVER ()) - actual AS Remaining
#2
5
Idan is 100% correct. I want to provide an explanation of this:
Idan 100%正确。我想提供一个解释:
(SUM (Auction) OVER ())
-
OVER ()
creates a window including all rows from the original query. - OVER()创建一个窗口,其中包含原始查询中的所有行。
-
SUM (Auction)
is a window function that calculates the sum ofAuction
. - SUM(拍卖)是一个窗口函数,用于计算拍卖总和。
Here is further explanation:
这是进一步的解释:
-
Window is a user-specified set of rows within a query result set.
Window是查询结果集中用户指定的一组行。
-
Window Function computes a value (e.g.
SUM
) over all rows in the window.窗口函数计算窗口中所有行的值(例如SUM)。
-
All rows are in the window because
OVER()
does not includePARTITION BY
. WithPARTITION BY
the window would include a subset of rows.所有行都在窗口中,因为OVER()不包括PARTITION BY。使用PARTITION BY窗口将包含行的子集。
来自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.
... OVER子句定义查询结果集中的窗口或用户指定的行集。然后,窗口函数计算窗口中每行的值。您可以将OVER子句与函数一起使用来计算聚合值...如果OVER子句不包含PARTITION BY。这意味着该函数将应用于查询返回的所有行。