曾经,有个用户提出了以下的一个问题,随即有很多朋友提供了他们的解决方案,我们的专家将这些记录整理出来,供大家参考学习。
问:要求把表B按照CLM001分组求和计算score的值,然后加到表A中对应CLM001的score的值。如何写更新表A的SQL语句?
表A:
CLM001 SCORE
001 0
002 0
003 0
004 0
其中CLM001是主键。
表B:
ID CLM001 SCORE
0 001 12
1 001 24
2 002 110
3 004 220
4 004 20
答:
|
with r as { select CLM001 ,sum(score) as total from b group by CLM001 } update a set score = (select total from r where r.CLM001 =a.CLM001) where exists (select 1 from r where r.CLM001 =a.CLM001) 版权申明:未经书面授权请勿转载本站信息!!作品版权归所属媒体与作者所有!!
2003-2005 Ksw123.com All Rights Reserved. - TOP
Copyright © 2006 Ksw123.com. All rights reserved.中国考题网 版权所有
|