Sql server SQL中具有两个表的递归查询

Sql server SQL中具有两个表的递归查询,sql-server,recursion,parent-child,Sql Server,Recursion,Parent Child,我有两个表,我想做一个递归查询,以便得到一个最终的表,其中包含一个父子关系和一个列,表示它是否有child。我的想法是连接这两个表,并使用use-CTE(查询给我一个错误,我附加了它)和我想要做的事情的图像 说明 表1和表2显示了帐户和操作的状态,表1显示了所有“产品”999,表2显示了所有“产品”39。其主要思想是在表1中使用“Product”999查找帐户和操作的来源,例如: 在表1中 首先,Product\u Ini=999/Acccount\u Ini=777/Operation\u

我有两个表,我想做一个递归查询,以便得到一个最终的表,其中包含一个父子关系和一个列,表示它是否有child。我的想法是连接这两个表,并使用use-CTE(查询给我一个错误,我附加了它)和我想要做的事情的图像

说明 表1和表2显示了帐户和操作的状态,表1显示了所有“产品”999,表2显示了所有“产品”39。其主要思想是在表1中使用“Product”999查找帐户和操作的来源,例如:

在表1中

  • 首先,Product\u Ini=999/Acccount\u Ini=777/Operation\u Ini=888的行来自Product\u Fin=999/Acccount\u Fin=777/Operation\u Fin=666
  • 第二,我的想法是查找Product\u Fin=999/Account\u Fin=777/Operation\u Fin=666的来源,为了做到这一点,我在同一个表中做了一个CTE,我发现Product\u Fin=39/Account\u Fin=126/Operation\u Fin=333
  • 第三,我们的想法是查找Product\u Fin=39/Account\u Fin=126/Operation\u Fin=333的来源,为了做到这一点,我必须使用表2并在那里搜索,结果是Product\u Fin=32/Account\u Fin=126/Operation\u Fin=858
  • 最后,我想得到一个显示帐户演变的表,其中有一个列Child,表示帐户/操作是否有Child

查询:

drop table #table_1
go
create table #table_1
(
    Product_Ini int,
    Account_Ini int,
    Operation_Ini   int,
    Product_Fin int,
    Account_Fin int,
    Operation_Fin int
)
go

drop table #table_2
go
create table #table_2
(
    Product_Ini int,
    Account_Ini int,
    Operation_Ini   int,
    Product_Fin int,
    Account_Fin int,
    Operation_Fin int
)
go

insert into #table_1 values (999, 123, 456, 32  ,123, 426)
insert into #table_1 values (999, 123, 456, 23  ,123, 159)
insert into #table_1 values (999, 123, 456, 65  ,123, 486)
insert into #table_1 values (999, 596, 162, 32  ,596, 263)
insert into #table_1 values (999, 126, 529, 999 ,126, 459)
insert into #table_1 values (999, 126, 459, 32  ,126, 784)
insert into #table_1 values (999, 126, 741, 999 ,126, 852)
insert into #table_1 values (999, 126, 852, 999 ,126, 111)
insert into #table_1 values (999, 126, 111, 999 ,126, 333)
insert into #table_1 values (999, 126, 333, 32  ,126, 995)
insert into #table_1 values (999, 523, 542, 999 ,523, 478)
insert into #table_1 values (999, 777, 888, 999 ,777, 666)
insert into #table_1 values (999, 777, 666, 39  ,126, 333)
insert into #table_1 values (999, 899, 565, 39  ,899, 474)
insert into #table_1 values (999, 565, 145, 39  ,565, 424)
insert into #table_1 values (999, 565, 361, 85  ,565, 452)
----
insert into #table_2 values (39, 126, 333, 32   ,126, 858)
insert into #table_2 values (39, 899, 474, 999  ,899, 525)
insert into #table_2 values (39, 565, 424, 999  ,565, 361)
--

select * from #table_1
select * from #table_2

;WITH ctetable(depth, Product_Ini, Account_Ini, Operation_Ini, Product_Fin, Account_Fin, Operation_Fin) as 
(
    SELECT 1 as depth, Product_Ini, Account_Ini, Operation_Ini, Product_Fin, Account_Fin, Operation_Fin
    FROM #table_1 as a 
    UNION ALL
    SELECT b.depth + 1 AS depth, b.Product_Ini, b.Account_Ini, b.Operation_Ini, c.Product_Fin, c.Account_Fin, c.Operation_Fin
    FROM ctetable AS b JOIN #table_1 as c on c.Product_Ini = b.Product_Fin and c.Account_Ini = b.Account_Fin and c.Operation_Ini = b.Operation_Fin
)
SELECT * 
--INTO #TMP_FINAL
FROM CTETABLE 
order by Account_Ini, Operation_Ini
GO
表格

发布ddl和样本数据的好工作。你能解释一下输出吗?我不懂“Childs”专栏。那是什么节目?我不太明白这两张表之间的关系。例如,表1中有两行Account_ini为777,但输出中有3行。第三排从哪里来?新的一行刚刚出现,但不知道为什么。@EGSL,你用详细的数据和预期的结果很好地回答了这个问题,如果你能提供一些关于#表#1和#表#2之间关系的信息,这可能会让我们更好地了解接下来要做什么。@SeanLange我补充了一些细节。提前谢谢。@jyao我添加了一些细节。提前谢谢。