我有一个游标,它从不同的表中收集信息,然后更新摘要,这是让我头疼的代码片段。
我正在尝试通过输入正确的男性和女性信息来更新汇总表,这些信息将加起来等于患者总数。
BEGIN
Set @MaleId =154
set @Femaleid =655
if @Category =@MaleId
begin
set @Males = @Value
end
if @Category = @Femaleid
begin
set @CummFemalesOnHaart =@Value
end
Update Stats
set Males =@Malest,Females =@Females
where
Category =@Category and
periodType =@PeriodType AND StartDate =@Startdate and EndDate =@enddate
问题:
结果不准确
organisation PeriodType StartDate EndDate Deaths Patients Males Females
------------ ---------- ---------- ---------- ------ -------- ----- -------
34 Monthly 2010-04-01 2010-04-30 NULL 6843 896 463
34 Monthly 2010-04-01 2010-04-30 NULL 10041 896 463
34 Monthly 2010-05-01 2010-05-31 NULL 10255 898 463
34 Monthly 2010-05-01 2010-05-31 NULL 7086 898 461
34 Monthly 2010-06-01 2010-06-30 NULL 10344 922 461
36 Monthly 2010-03-01 2010-03-31 NULL 4317 1054 470
36 Monthly 2010-03-01 2010-03-31 NULL 5756 896 470
36 Monthly 2010-04-01 2010-04-30 NULL 4308 896 463
36 Monthly 2010-04-01 2010-04-30 NULL 5783 896 463
男性和女性应该只更新加起来等于患者总数的单行,例如
Patients Males Females
-------- ----- -------
41 21 20
有任何想法吗?
只是为了澄清一点
/查询表/
organisation PeriodType StartDate EndDate Males Females
------------ ---------- ---------- ---------- ----- -------
34 Monthly 01-02-2012 01-02-2012 220 205
34 Monthly 01-02-2012 01-02-2012 30 105
/*更新语句*/
Update Stats
set Males =@Malest,Females =@Females
where
--Category =@Category and
periodType =@PeriodType AND StartDate =@Startdate and EndDate =@enddate
统计表 /输入前/
organisation PeriodType StartDate EndDate Deaths Patients Males Females
------------ ---------- ---------- ---------- ------ -------- ----- -------
34 Monthly 01-02-2012 01-02-2012 0 425 null null
34 Monthly 01-02-2012 01-02-2012 25 null null null
34 Monthly 01-02-2012 01-02-2012 5 null null null
34 Monthly 01-02-2012 01-02-2012 5 135 null null
/如果您仔细查看期间类型、开始日期、结束日期的行,那么我不希望更新值影响除患者总数之外的任何其他行/
统计表 /* 输出 */
organisation PeriodType StartDate EndDate Deaths Patients Males Females
------------ ---------- ---------- ---------- ------ -------- ----- -------
34 Monthly 01-02-2012 01-02-2012 0 425 220 205
34 Monthly 01-02-2012 01-02-2012 25 null null null
34 Monthly 01-02-2012 01-02-2012 5 null null null
34 Monthly 01-02-2012 01-02-2012 5 135 30 105
我希望这能提供更多信息