读书人

求一SQL语句解决方案

发布时间: 2012-03-16 16:34:56 作者: rapoo

求一SQL语句
表如下:
ID DATE
1001 1
10010001 2
10010002 3
1002 4
10020001 5

四位一个标识.来做计算
即10010001 ,10010002 为1001的子
10020001 为1002的子

现想把date字段中的值一个主的所有子的值加到主值中.应怎么样写SQL
结果应如下
ID DATE
1001 6
10010001 2
10010002 3
1002 9
10020001 5


[解决办法]

update table set date= d.counts from table c,
(select date,(select sum(date)from table b where charindex( a.identifier,b.identifier)>0)As counts from table a)d
where d.identifier = c.identifier

读书人网 >C++ Builder

热点推荐