【问题标题】:Running Totals Results Incorrect运行总计结果不正确
【发布时间】: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


【解决方案1】:

据我所知,您所提议的内容始终存在分配不正确数据的风险。如果您有两条记录,一条包含 500 名患者(男性 250 名和女性 250 名),另一份包含 500 名患者(但男性 300 名和女性 200 名),会发生什么?

没有明确的区别然后使用哪个记录总数,您最终可能会使用不正确的数据进行多次更新。

话虽如此,我认为你应该做的是以下。

  1. 创建一个 CTE,从 stats 表中选择患者,从查询表中选择男性/女性,其中患者 =(男性 + 女性)

  2. 在统计表上运行更新语句,使用 CTE 中的值加入患者总数。

据我所知,这应该可以解决您所追求的问题。

您可以在此处阅读有关 CTE 的更多信息:MSDN Common Table Expressions

【讨论】:

    【解决方案2】:

    我不完全确定我理解你的问题......但认为这就是你所追求的吗?

    UPDATE Stats
    SET Males = (SELECT COUNT(*) From <queryTable> WHERE Category = @MaleId and periodType =@PeriodType AND StartDate =@Startdate and EndDate =@enddate)
    ,SET Females = (SELECT COUNT(*) From <queryTable> WHERE Category = @FemaleId and periodType =@PeriodType AND StartDate =@Startdate and EndDate =@enddate)
    

    希望对你有帮助!

    【讨论】:

    • 感谢您的回复,我已经添加了更多信息,可能会更加清晰
    【解决方案3】:

    我也不确定我是否理解这个问题。但听起来你是说可以有多个记录具有相同的periodTypestartDateendDate,但Patients 的数量不同。您希望确保查询仅更新 Patients 的总数与您计算的总数匹配的记录,即总数 = @Males + @Females

    如果正确,请在“患者”值上添加过滤器:

        UPDATE Stats 
        SET    Males = @Males,
               Females = @Females 
        WHERE  Category = @Category 
        AND    periodType = @PeriodType 
        AND    StartDate = @Startdate 
        AND    EndDate = @enddate 
        AND    Patients = (@Males + @Females)
    

    顺便说一句,游标通常比基于集合的替代方案效率低..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2016-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      相关资源
      最近更新 更多