用这个肯定可以的.update 你的表 set value2=(select sum(value1) from 你的表 where date_id<=a.date_id,area_id=a.area_id) from 你的表 a --------------------------- 语法错误!!!!
误将and写成了逗号,改正:update 你的表 set value2=(select sum(value1) from 你的表 where date_id<=a.date_id and area_id=a.area_id) from 你的表 a
表结构: DATE_ID char no 8 no no no Chinese_PRC_CI_AS DOMAIN_ID decimal no 9 18 0 no (n/a) (n/a) NULL AGE_ID decimal no 5 3 0 no (n/a) (n/a) NULL PROG_TYPE_ID char no 6 no no no Chinese_PRC_CI_AS ICP_ID char no 8 no no no Chinese_PRC_CI_AS BILL_TYPE_ID decimal no 5 2 0 no (n/a) (n/a) NULL USRS_TOTAL decimal no 9 10 0 yes (n/a) (n/a) NULL USRS_NEW decimal no 5 8 0 yes (n/a) (n/a) NULL USRS_END decimal no 9 18 0 yes (n/a) (n/a) NULL USRS_ADD decimal no 5 8 0 yes (n/a) (n/a) NULL ———————————————————————————————————— 一些数据: 20030101 4141 3 3030 2656 3 1000000 1686 1323 1000000 20030101 4091 3 4085 48 1 1000000 1261 626 1000000 20030101 4137 3 4272 48 3 1000000 1606 1142 1000000 20030101 4038 5 3001 2695 3 1000000 1048 585 1000000 20030102 4172 2 4327 82 1 117 1397 1280 117 20030102 4152 3 1 27 2 1236 1737 501 1236 20030102 4031 1 4096 2656 0 422 1389 967 422 20030102 4337 5 4052 27 3 641 1208 567 641 20030102 1059 5 3048 2695 1 920 1515 595 920 20030102 4280 4 4194 2656 4 361 1842 1481 361 20030102 4047 5 4277 2652 0 -120 1245 1365 -120 20030102 4095 2 3045 2656 1 237 1675 1438 237 20030102 101042 1 4277 31 4 283 1385 1102 283 20030103 4269 5 3051 2695 3 690 1683 993 690 20030103 4365 6 4114 2695 0 310 1321 1011 310 20030103 1054 4 4108 2652 1 365 1738 1373 365 20030103 4225 1 4265 12 0 51 1008 957 51 20030103 4213 3 4190 82 4 889 1459 570 889 20030103 3042 2 4113 2656 1 1040 1962 922 1040 _______________________________________________________________________ 我的sql: update fact_usrs_app set usrs_total=(select sum(usrs_add) from fact_usrs_app a where convert(datetime,a.date_id)<=convert(datetime,date_id) and domain_id=a.domain_id and age_id=a.age_id and prog_type_id=a.prog_type_id and icp_id=a.icp_id and bill_type_id=a.bill_type_id)
试试: update fact_usrs_app set usrs_total=(select sum(usrs_add) from fact_usrs_app where convert(datetime,date_id,120)<=convert(datetime,a.date_id,120) and domain_id=a.domain_id and age_id=a.age_id and prog_type_id=a.prog_type_id and icp_id=a.icp_id and bill_type_id=a.bill_type_id) from fact_usrs_app a
update fact_usrs_app set usrs_total=(select sum(usrs_add) from fact_usrs_app a where datediff(day,rtrim(a.date_di),rtrim(fact_usrs_app.date_di))>=0 and domain_id=a.domain_id and age_id=a.age_id and prog_type_id=a.prog_type_id and icp_id=a.icp_id and bill_type_id=a.bill_type_id)
update fact_usrs_app set usrs_total=(select sum(usrs_add) from fact_usrs_app a where datediff(day,rtrim(a.date_di),rtrim(fact_usrs_app.date_di))>=0 and fact_usrs_app.domain_id=a.domain_id and fact_usrs_app.age_id=a.age_id and fact_usrs_app.prog_type_id=a.prog_type_id and fact_usrs_app.icp_id=a.icp_id and fact_usrs_app.bill_type_id=a.bill_type_id)
set value2=value1+(select sum(value1) from 表 where area_id =a.area_id and convert(datetime,date_id)<=convert(datetime,a.date_id))
from 表 a
set value2=(select sum(value1) from 你的表 where date_id<=a.date_id,area_id=a.area_id)
from 你的表 a
有问题吗?
set value2=(select sum(value1) from 你的表 where date_id<=a.date_id,area_id=a.area_id)
from 你的表 a
insert #a values(20021201, 1 , 100, 0)
insert #a values(20021201, 2 , 212,0)
insert #a values( 20021201 , 3 , 263,0)
insert #a values(20021202 , 1 , 221 ,0)
insert #a values( 20021202 , 2 , 223,0)
insert #a values( 20021202 , 3 , 163,0)
insert #a values( 20021203 ,1 , 45 ,0)
insert #a values( 20021203 ,2 , 76,0)
insert #a values( 20021203 ,3, 567,0)
update #a set value2=(select sum(value1) from #a 别名 where 别名.area_id=#a.area_id and 别名.date_id<=#a.date_id)select * from #ago
drop table #adate_id area_id value1 value2
---------- ---------- ----------- ------------
20021201 1 100 100
20021201 2 212 212
20021201 3 263 263
20021202 1 221 321
20021202 2 223 435
20021202 3 163 426
20021203 1 45 366
20021203 2 76 511
20021203 3 567 993(所影响的行数为 9 行)
set value2=(select sum(value1) from 你的表 where date_id<=a.date_id,area_id=a.area_id)
from 你的表 a
---------------------------
语法错误!!!!
set value2=(select sum(value1) from 你的表 where date_id<=a.date_id and area_id=a.area_id)
from 你的表 a
create table #a(date_id char(10),area_id decimal(8),value1 int,value2 decimal(10))
insert #a values(20021201, 1 , 100, 0)
insert #a values(20021201, 2 , 212,0)
insert #a values( 20021201 , 3 , 263,0)
insert #a values(20021202 , 1 , 221 ,0)
insert #a values( 20021202 , 2 , 223,0)
insert #a values( 20021202 , 3 , 163,0)
insert #a values( 20021203 ,1 , 45 ,0)
insert #a values( 20021203 ,2 , 76,0)
insert #a values( 20021203 ,3, 567,0)--更新数据
update #a
set value2=(select sum(value1) from #a where date_id<=a.date_id and area_id=a.area_id)
from #a aselect * from #adrop table #a
---------- ---------- ----------- ------------
20021201 1 100 100
20021201 2 212 212
20021201 3 263 263
20021202 1 221 321
20021202 2 223 435
20021202 3 163 426
20021203 1 45 366
20021203 2 76 511
20021203 3 567 993(所影响的行数为 9 行)
DATE_ID char no 8 no no no Chinese_PRC_CI_AS
DOMAIN_ID decimal no 9 18 0 no (n/a) (n/a) NULL
AGE_ID decimal no 5 3 0 no (n/a) (n/a) NULL
PROG_TYPE_ID char no 6 no no no Chinese_PRC_CI_AS
ICP_ID char no 8 no no no Chinese_PRC_CI_AS
BILL_TYPE_ID decimal no 5 2 0 no (n/a) (n/a) NULL
USRS_TOTAL decimal no 9 10 0 yes (n/a) (n/a) NULL
USRS_NEW decimal no 5 8 0 yes (n/a) (n/a) NULL
USRS_END decimal no 9 18 0 yes (n/a) (n/a) NULL
USRS_ADD decimal no 5 8 0 yes (n/a) (n/a) NULL
————————————————————————————————————
一些数据:
20030101 4141 3 3030 2656 3 1000000 1686 1323 1000000
20030101 4091 3 4085 48 1 1000000 1261 626 1000000
20030101 4137 3 4272 48 3 1000000 1606 1142 1000000
20030101 4038 5 3001 2695 3 1000000 1048 585 1000000
20030102 4172 2 4327 82 1 117 1397 1280 117
20030102 4152 3 1 27 2 1236 1737 501 1236
20030102 4031 1 4096 2656 0 422 1389 967 422
20030102 4337 5 4052 27 3 641 1208 567 641
20030102 1059 5 3048 2695 1 920 1515 595 920
20030102 4280 4 4194 2656 4 361 1842 1481 361
20030102 4047 5 4277 2652 0 -120 1245 1365 -120
20030102 4095 2 3045 2656 1 237 1675 1438 237
20030102 101042 1 4277 31 4 283 1385 1102 283
20030103 4269 5 3051 2695 3 690 1683 993 690
20030103 4365 6 4114 2695 0 310 1321 1011 310
20030103 1054 4 4108 2652 1 365 1738 1373 365
20030103 4225 1 4265 12 0 51 1008 957 51
20030103 4213 3 4190 82 4 889 1459 570 889
20030103 3042 2 4113 2656 1 1040 1962 922 1040
_______________________________________________________________________
我的sql:
update fact_usrs_app
set usrs_total=(select sum(usrs_add) from fact_usrs_app a
where convert(datetime,a.date_id)<=convert(datetime,date_id) and
domain_id=a.domain_id and age_id=a.age_id and
prog_type_id=a.prog_type_id and
icp_id=a.icp_id and
bill_type_id=a.bill_type_id)
update fact_usrs_app
set usrs_total=(select sum(usrs_add) from fact_usrs_app
where convert(datetime,date_id,120)<=convert(datetime,a.date_id,120) and
domain_id=a.domain_id and age_id=a.age_id and
prog_type_id=a.prog_type_id and
icp_id=a.icp_id and
bill_type_id=a.bill_type_id)
from fact_usrs_app a
and fact_usrs_app.age_id=a.age_id
and fact_usrs_app.prog_type_id=a.prog_type_id
and fact_usrs_app.icp_id=a.icp_id
and fact_usrs_app.bill_type_id=a.bill_type_id)