简体   繁体   English

SQL Server加入复杂子查询

[英]SQL Server join complex subquery

I am trying to combine my query into a join to bring back some records. 我试图将我的查询结合到一个连接中以带回一些记录。

Here is the first part of my query, this does exactly what I want and returns the latest record from this result set as each record is linked to the other by an fkSDSID as show below, the first one does not have an id as its the first one and there are no previous records. 这是我的查询的第一部分,这正是我想要的,并返回此结果集中的最新记录,因为每个记录通过fkSDSID链接到另一个,如下所示,第一个没有id作为其第一个,没有以前的记录。

pkSDSID   fkSDSID
 50605     NULL
 88377     50605
 90602     88377
 90616     90602

This query returns 90616 from the results. 此查询从结果中返回90616。

DECLARE @LatestSDS INT

with tree as (
   SELECT pkSDSID, fkSDSID
   FROM tblSDS
   WHERE pkSDSID = 50605

   UNION ALL

   SELECT t1.pkSDSID, t1.fkSDSID
   FROM tblSDS t1
   JOIN tree p ON p.pkSDSID = t1.fkSDSID
)

SELECT TOP 1 @LatestSDS = pkSDSID FROM tree ORDER BY pkSDSID DESC

SELECT @LatestSDS

This is fine if I have one record I want to find but what I'm now stuck on his how to do this for multiple records. 如果我有一个我想要找到的记录,但我现在仍然坚持他如何为多个记录执行此操作,这很好。

I was wondering if I could inner join this somehow using it as a sub query of my main query but I cant seem to find a way to get it to work. 我想知道我是否可以以某种方式内部加入它作为我的主要查询的子查询但我似乎无法找到一种方法让它工作。

What I would like is to replace the hard coded pkSDSID in the first where clause like above which is 50605 and instead use a column or a number of records and get the LastestSDS for each record. 我想要的是在上面的第一个where子句pkSDSID中替换硬编码的pkSDSID ,而是使用一列或多个记录并获得每个记录的LastestSDS

An example being this 这是一个例子

I have two records say 50605 and 45670. 我有两个记录说50605和45670。

They both have newer records like so in the table 他们都有更新的记录,如表中所示

pkSDSID   fkSDSID
 50605     NULL
 88377     50605
 90602     88377
 90616     90602

pkSDSID   fkSDSID
 45670     NULL
 50123     45670
 51234     50123
 60125     51234

So for each of these records I need to use my code above to get the latest record which would be 90616 and 60125 respectively. 因此,对于每个记录,我需要使用上面的代码来获取最新记录,分别为90616和60125。

Then display just these new records in a list. 然后在列表中显示这些新记录。

I hope this makes sense I'm not all that great with SQL and I just don't know where to go from here. 我希望这是有道理的,我对SQL并不是那么好,我只是不知道从哪里开始。

Is this actually possible? 这有可能吗?

Thanks Dan 谢谢丹

Edit, after Q update Q更新后编辑

DECLARE @t TABLE (pkSDSID int NOT NULL, fkSDSID int NULL);
INSERT @t VALUES
(50605, NULL),
(88377, 50605),
(90602, 88377),
(90616, 90602),
(45670, NULL),
(50123, 45670),
(51234, 50123),
(60125, 51234);

with tree as (
   SELECT S.pkSDSID, S.fkSDSID, 0 AS TreeLevel, S.pkSDSID AS TreeTop
   FROM @t S
   WHERE S.pkSDSID IN (50605, 45670)

   UNION ALL

   SELECT t1.pkSDSID, t1.fkSDSID, TreeLevel +1, p.TreeTop
   FROM @t t1
   JOIN tree p ON p.pkSDSID = t1.fkSDSID
)
, Filter AS
(
SELECT
     pkSDSID,
     ROW_NUMBER() OVER (PARTITION BY TreeTop ORDER BY TreeLevel DESC) AS rn
 FROM tree
)
SELECT pkSDSID FROM Filter WHERE rn = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM