【发布时间】:2012-05-18 00:00:49
【问题描述】:
我有一个游标,它从不同的表中收集信息,然后更新摘要,这是让我头疼的代码 sn-p。
我正在尝试通过输入正确的男性和女性信息来更新汇总表,这些信息将加起来为患者总数。
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
男性和女性应该只更新加起来等于患者总数的单行 例如,g
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
我希望这能提供更多信息
【问题讨论】:
-
为了让代码脱颖而出,编辑您的问题,突出显示代码,然后按此按钮
{},它是 B 的第 5 个按钮,我,...。你可以对结果做同样的事情 -
如果您自始至终使用 same 示例数据,会更容易理解。首先向我们展示原始数据。然后在更新后显示完全相同的记录(即“错误”结果)。最后,数据应该是什么 - 以及为什么。
标签: sql sql-server-2008 tsql