开发者

SQL Server add count column near existing select columns

开发者 https://www.devze.com 2023-02-02 18:57 出处:网络
i have a query that returns something like this. ID | Company| Total_Money | no_items | count_Total_Money_For_Company

i have a query that returns something like this.

ID | Company| Total_Money | no_items | count_Total_Money_For_Company
--------------------------------------------------------------------
1  | A      | 1000        | 1        | 2001
2  | A      | 1001        | 开发者_运维问答0        | 2001
3  | B      | 1001        | 1        | 5010
4  | B      | 1002        | 1        | 5010
5  | B      | 1003        | 1        | 5010
6  | B      | 1004        | 1        | 5010
7  | B      | 1000        | 1        | 5010

How can i add that column with the count for that company?


Try this(Uses SUM Aggregate Function):

SELECT id, 
       company, 
       total_money, 
       no_items, 
       SUM(total_money) OVER(PARTITION BY id) count_total_money_for_company 
FROM   <your_table >


try this

SELECT ID, 
       Company, 
       Total_Money, 
       no_items, 
       SUM(Total_Money) OVER(PARTITION BY Company) count_Total_Money_For_Company 
FROM   Company


Here is the solution for your problem..

Use the SUM() sql function.. You can use it with a groupby

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号