在Oracle中按名称添加新列SUM Money按名称

时间:2018-09-07 10:32:13

标签: sql oracle sum

员工表:

Name | Can_use | Money    
Jack | Yes     | 1000    
Lara | Yes     | 1500    
Jack | No      | 500    
Jack | Yes     | 700    
Lara | Yes     | 500

我要创建一个View表,其结果为:

Name | Can_use | Money | Sum    
Jack | Yes     | 1000  | 1700    
Lara | Yes     | 1500  | 2000    
Jack | No      | 500   | 1700    
Jack | Yes     | 700   | 1700    
Lara | Yes     | 500   | 2000

使用Employy“ Can_use” =“是”,然后加上+ =金钱

救救我!

1 个答案:

答案 0 :(得分:4)

SUM窗口函数与CASE一起可以解决您的问题。

CREATE OR REPLACE VIEW yourView AS
SELECT Name,
       Can_use,
       Money,
       SUM(CASE WHEN Can_use = 'Yes' THEN Money ELSE 0 END) OVER (PARTITION BY Name) "Sum"
FROM yourTable;