【问题标题】:Update a whole column based on 2 columns of a different table根据不同表的 2 列更新整个列
【发布时间】:2012-10-06 23:28:54
【问题描述】:

我在表格中添加了一个新列。该表的列是:

Identifier(unique identity but not joinable to the other table)
ClinicalID(int)
senttime(datetime)
orgID(int-new column)

我需要根据发送时间从不同的表中更新新列。 我应该检查发送时间落在哪个日期范围并相应地更新 orgID。 另一张表的列:

ClinicalIDentifier
org ID(int) 
old orgID(int)
effectivefromtime
effectivethrutime

我面临的问题是,如果发送时间 > 从另一个表的有效开始时间,它会显示发送时间 > 有效开始时间的相同临床 ID 的其他组织 ID。

我一直在比较日期范围。

如果您需要更多信息,请告诉我

【问题讨论】:

  • 你试过between吗? senttime between effectivefromtime and effectivethrutime
  • 是的。我试过了。还是一样。它还接受小于该值的值,并在使用 between 查询时显示空值。
  • 我正在寻找的是,如果 senddate 在两个 otehr 日期列之间,那么 org Id 或者旧的 org ID...我已经使用了 case 语句:codecase when (op .effectivethrutime 不为 null 且 ac.senttime 在 op.effectivefromtime 和 op.effectivethrutime 之间)然后 op.organizationidentifier 当(op.effectivethrutime 不为 null 并且 ac.senttime

标签: sql sql-server-2005 datetime


【解决方案1】:

知道了!!

select  
case when (effectivethrutime is null and senttime >= effectivefromtime) then orgid  
when (effectivethrutime is not null and senttime >= effectivefromtime) then orgID  
when (effectivethrutime is not null and senttime between effectivethrutime and   effectivefromtime) then orgid  
end as orgid  
from ac  
left join op  
on ac.clinicalidentifier=op.clinicalidentifier  
where ac.senttime>op.effectivefromtime   
and ac.senttime<=isnull(op.effectivethrutime,ac.senttime)

目前有效。
如果需要更正,请告诉我。
谢谢
维哈尔

【讨论】:

    【解决方案2】:
    WITH CTE AS
    (
    SELECT
        FirstTable.OrgID NewOrgID,
        SecondTable.OrgID
    FROM
        FirstTable
        LEFT JOIN SecondTable 
            ON FirstTable.ClinicalID = SecondTable.ClinicalIndetifier 
            AND FirstTable.SentTime > SecondTable.EffectiveFromTime 
            AND FirstTable.SentTime < SecondTable.EffectiveThruTime
    )
    UPDATE CTE
        SET NewOrgID = OrgID
    

    我无法从您的问题中看出表名是什么或它们是如何连接的,但我认为应该这样做。要测试 CTE,您应该将上面的整个 UPDATE 语句替换为 SELECT * FROM CTE,这将显示每个 NewOrgID 为 NULL,而 OrgID 将显示将在每一行中填充的值。如果您想确定可以修改 CTE,使其包含 FirstTable 和 SecondTable 中的所有列。

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2020-06-18
      • 1970-01-01
      相关资源
      最近更新 更多