简体   繁体   English

SQL更新,每条记录有多个更新

[英]SQL Update with Multiple updates per Record

I am trying to update months of a given persons records to reduce their potential if they get a claim within a month. 我正在尝试更新给定人员记录的月份,以减少他们在一个月内提出索赔的潜力。

UPDATE M 
SET POTENTIAL_HITS = POTENTIAL_HITS - 1  
FROM #TEMP_CDC_MEMBERS M 
INNER JOIN #TEMP_CDC_CLAIMS C ON M.CIN = C.CIN AND C.MEASURE_INDICATOR = M.SUB_MEASURE
WHERE M.MOE > C.MOE

The MOE is a month field in a 201607 format. MOE是201607格式的月份字段。

The problem I am having is if there is a hit in one month, that difference will be ignored in a later month (due to SQL transactions). 我遇到的问题是,如果一个月内有命中,那么该差异将在下个月被忽略(由于SQL事务)。

Is there a way to update the potential without using a loop? 有没有不用循环就可以更新电位的方法?

Your problem is that update doesn't do cumulative updates -- only one update per record in m . 您的问题是update不执行累积更新m每条记录仅更新一次。 I think you can do what you want using cross apply : 我认为您可以使用cross apply做您想做的事情:

UPDATE M 
SET POTENTIAL_HITS = POTENTIAL_HITS - c.cnt
FROM #TEMP_CDC_MEMBERS M CROSS APPLY
     (SELECT COUNT(*) as cnt
      FROM #TEMP_CDC_CLAIMS C 
      WHERE M.CIN = C.CIN AND C.MEASURE_INDICATOR = M.SUB_MEASURE AND M.MOE > C.MOE) c;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM