在表数据之前:
-----------------------
|ID |WORK|VALUE|TOTAL|
-----------------------
|ID1|WRITE |10 | |
|ID1|TYPE |5 | |
|ID2|READ |25 | |
|ID2|SCAN |30 | |
|ID3|PRINT |15 | |
|ID4|SETTING|20 | |
|ID5|REPAIR |5 | |
|ID5|MAINTE |25 | |
|ID5|MONITOR|20 | |
总计是来自同一身份的总和值
ID1 10+5
ID2 25+30
ID3 15
ID4 20
ID5 50
现在我将insert方法与create table data2(ID,TOTAL)
一起使用INSERT INTO DATA2(DATA2.ID, DATA2.TOTAL)
SELECT DATA.ID, SUM (DATA.VALUE) AS TOTAL
FROM DATA
GROUP BY DATA.ID
然后我做SELECT JOIN FROM DATA2 AND DATA
表数据后
-----------------------
|ID |WORK|VALUE|TOTAL|
-----------------------
|ID1|WRITE |10 |15 |
|ID1|TYPE |5 |15 |
|ID2|READ |25 |55 |
|ID2|SCAN |30 |55 |
|ID3|PRINT |15 |15 |
|ID4|SETTING|20 |20 |
|ID5|REPAIR |5 |50 |
|ID5|MAINTE |25 |50 |
|ID5|MONITOR|20 |50 |
答案 0 :(得分:0)
要将值total更新为table,您需要拥有列..
alter table tblname
add total int
如果这不是一次性的方法,我建议创建一个如下所示的视图
create view somename
as
select id,work,value,
sum(value) over (partition by id ) as total
from tabel
如果您想将表更新为一次性练习
;with cte
as
(select id,work,value,
sum(value) over (partition by id ) as total1
from tablename
)
update t
set t.total=c.total1
from cte c
join tablename t
on t.id=c.id
答案 1 :(得分:0)