Sql 从没有时间戳的历史记录表中检索上一个客户名称

Sql 从没有时间戳的历史记录表中检索上一个客户名称,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有以下两个表格: 顾客: |Customer Code|Current Customer Code| |123456|-| --------------- |123455|-| --------------- |123454|-| --------------- |123453|-| --------------- 历史: |customer Code x (current)|Customer Code y(former)| |123456|123455| --------------- |

我有以下两个表格:

顾客:

|Customer Code|Current Customer Code|
|123456|-|
---------------
|123455|-|
---------------
|123454|-|
---------------
|123453|-|
---------------
历史:

|customer Code x (current)|Customer Code y(former)|
|123456|123455|
---------------
|123455|123454|
---------------
|123454|123453|
---------------
|123453|123452|
---------------
|123452|      |
我想要的是:

|Customer Code|Current Customer Code|

|123456|123456|
---------------
|123455|123456|
---------------
|123454|123456|
---------------
|123453|123456|
---------------
此时,当前客户在customer表中是未知的。我需要从历史记录表中检索当前客户代码,并将其写入客户表中的当前客户字段

我做到了这一点:

SELECT 
[Customer Code], [Current Customer Code] FROM customers
LEFT JOIN(
    SELECT 
    c.[Customer Code],
    t.dats
    FROM Customers c
        inner JOIN (
            SELECT [Customer Code x], [Customer Code y] FROM 
            history t
            LEFT JOIN Customers c
            ON t.bpid = c.[customer code]
       ) t
   ON c.[Customer Code] = t.[Customer Code]
请注意:

  • 可以有多个客户集,但这些客户集将显示在历史记录表中
  • 数字可能会有跳跃,例如从12455跳到12453

[[答案更新-说明OPs修订的源数据]]

@PreQL提到将数据视为层次结构

我在这里声明了几个表变量,以便对查询进行简单的剪切/粘贴/测试

declare @customers TABLE (CustomerCode int, CurrenctCustomerCode int)

insert into @customers values (123456, null)
insert into @customers values (123455, null)
insert into @customers values (123454, null)
insert into @customers values (123453, null)

declare @history TABLE (CustomerCodeX int, CustomerCodeY int)

insert into @history values (123456, 123455)
insert into @history values (123455, 123454)
insert into @history values (123454, 123453)
insert into @history values (123453, 123452)
insert into @history values (123452, null)
我们需要找到层次结构的定位点——我们可以通过一个子查询找到不是后续交易ID的“先前”ID的交易ID

   select c.* 
     from @customers c 
left join @history h 
       on c.CustomerCode = h.CustomerCodeY  
    where h.CustomerCodeX is null
将带有的子查询放入我的原始CTE中,如下所示:

; with cte (CCX, CCY, CCC) 
as
(

    select h1.CustomerCodeX, h1.CustomerCodeY, x.CustomerCode
    from @history h1
    join (select c.* from @customers c left join @history h on c.CustomerCode = h.CustomerCodeY  where h.CustomerCodeX is null) x
    on h1.CustomerCodeX = x.CustomerCode
    union all 
    select 
        h.CustomerCodeX,
        h.CustomerCodeY,
        cte.CCC
    from @history h
    join cte on h.CustomerCodeX = cte.CCY
)
select CCX as CustomerCode, CCC as CurrentCustomerCode from cte
输出:

CustomerCode CurrentCustomerCode
------------ -------------------
123456       123456
123455       123456
123454       123456
123453       123456
123452       123456

希望它有用。

您可以使用递归CTE来实现这一点

我的查询从历史记录中没有较新客户代码的所有实际客户代码开始。然后,它使用历史记录递归地添加以前的客户代码(空代码除外)

递归CTE创建如下列表:

; with cte (CCX, CCY, CCC) 
as
(

    select h1.CustomerCodeX, h1.CustomerCodeY, x.CustomerCode
    from @history h1
    join (select c.* from @customers c left join @history h on c.CustomerCode = h.CustomerCodeY  where h.CustomerCodeX is null) x
    on h1.CustomerCodeX = x.CustomerCode
    union all 
    select 
        h.CustomerCodeX,
        h.CustomerCodeY,
        cte.CCC
    from @history h
    join cte on h.CustomerCodeX = cte.CCY
)
select CCX as CustomerCode, CCC as CurrentCustomerCode from cte


UPDATE语句随后使用此列表更新客户表(如果需要,请调整MAXRECURSION选项)。

是一个很好的起点。我完全不明白你想做什么。查看历史记录时,从历史记录表中检索最新的当前客户。因此,该客户的当前客户代码是123456。而客户代码的历史可以追溯到123453。在我的客户表中,我想将当前客户代码的值设置为这些客户历史上最新的客户代码(在示例中:123456到123453),这很好。没有一些细节,我无能为力。从你发布的信息来看,我认为你没有太多机会,因为你没有发布任何数据来表明当前客户可能是什么。发布一些表定义和示例数据。然后试着解释你想要什么。你想要检索一个客户还是多个客户?另外,如果你从一个数字开始,预期的结果是什么?另一个号码?例如,一张桌子没有自然的顺序,所以没有最后一张。哈哈哈,我刚到家,开始着手处理这个问题。这正是我要发布的内容:p,+1我在想这样的事情!我只是没有到达那里的知识/技能。我将尝试看看它是否适用于完整的数据集。哦,不。。。我把历史表弄错了。我很抱歉。我在问题中更新了它。显然,客户代码和当前客户代码不存在相同的情况。@PreQL-很抱歉抢走了你的风头;-)哈哈哈,没问题,所有的评论和回答都是公平的:不。。。我把历史表弄错了。我很抱歉。我在问题中更新了它。显然,客户代码和当前客户代码并不相同。示例历史记录中的第一行仍然相同(将代码映射到自身),现在您似乎已经更改了新旧客户代码(X和Y)的顺序。历史记录中哪个是旧的,哪个是新的客户代码?请再次更新问题。抱歉,再次更新问题。第一栏是最新的,最后一栏是前一栏。谢谢,现在我可以把我的答案缩短一点了。:-)