-1

我遇到了我正在尝试解决的问题:每天我都会将新记录导入具有 ID 号的表中。

它们中的大多数是新的(以前从未在系统中看到过),但有些会再次出现。我需要做的是如果在存档中找到编号,则在 ID 编号的末尾附加一个字母,但前提是该行中的数据与存档中的数据不同,并且这需要按顺序完成, IE,如果第二次看到 12345 的数据不同,我将其更改为 12345A,如果再次看到 12345,并且再次不同,我需要将其更改为 12345B,等等。

最初我尝试使用一个where循环,它将所有“再次看到”的记录放在一个临时表中,然后第一次分配 A,然后删除那些,将 B 分配给剩下的,删除那些等等,直到临时表是空的,但这还没有解决。

或者,我一直在考虑尝试子查询,如:

update table
set IDNO= (select max idno from archive) plus 1

有什么建议么?

4

3 回答 3

0

Here is my final solution:

update a
set IDnum=b.IDnum
from tempimiportable A inner join 
    (select * from archivetable
     where IDnum in 
     (select max(IDnum) from archivetable
      where IDnum in 
       (select IDnum from tempimporttable)
      group by left(IDnum,7) 
      )
     ) b
on b.IDnum like a.IDnum + '%'
WHERE 
*row from tempimport table = row from archive table*

to set incoming rows to the same IDnum as old rows, and then

update a
set patient_account_number = case 
    when len((select max(IDnum) from archive where left(IDnum,7) = left(a.IDnum,7)))= 7 then a.IDnum + 'A'
    else left(a.IDnum,7) + char(ascii(right((select max(IDnum) from archive where left(IDnum,7) = left(a.IDnum,7)),1))+1)
    end
from tempimporttable a
where not exists ( *select rows from archive table* )

I don't know if anyone wants to delve too far into this, but I appreciate contructive criticism...

于 2011-07-12T13:19:46.360 回答
0

这个想法怎么样?请注意,这基本上是伪代码,因此请根据需要进行调整。

使用“src”作为最终将插入所有数据的表,并将“TMP”作为您的临时表..这是假设 TMP 中的 ID 列是double

do
    update tmp set id = id + 0.01 where id in (select id from src);
until no_rows_changed;

alter table TMP change id into id varchar(255);

update TMP set id = concat(int(id), chr((id - int(id)) * 100 + 64);

insert into SRC select * from tmp;
于 2011-07-07T21:04:37.393 回答
0

当你到达 12345Z 时会发生什么?

无论如何,稍微改变一下表结构,这里是配方:

  1. 删除任何索引ID

  2. ID将(显然是 varchar)拆分为ID_Num(long int)和ID_Alpha(varchar,not null)。ID_Alpha为空字符串 ( '')设置默认值。
    因此,12345B (varchar) 变为12345 (long int) 和'B' (varchar) 等。

  3. 在列ID_NumID_Alpha.
    将此作为主键。或者,如果必须,使用自动递增整数作为伪主键。

  4. 现在,在添加新数据时,查找重复的 ID 号是微不足道的,并且可以通过简单的max()操作获得最后一个 ID_Alpha。

  5. 使用 while 循环或游标(如果必须),现在解决重复 ID 应该是一项更容易的任务。
    但是,也应该可以避免“通过痛苦行来行”(RBAR),并使用基于集合的方法。几天阅读Jeff Moden 的文章,应该会给你这方面的想法。

于 2011-07-09T23:15:18.123 回答