员工表:
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” =“是”,然后加上+ =金钱
救救我!
答案 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;