如何获得包含公司(ID)的公司列表?

时间:2020-08-05 08:42:33

标签: sql-server tsql recursive-cte

重要...在我的RL项目中,ID不是INT,而是GUID,因此我的数据不是分层的!

我有一个包含公司的表格和一个包含公司之间链接的表格。

我需要能够从特定公司ID中检索公司列表。

这是我的测试代码...

CREATE TABLE ##corporations
(  
    CorporationID INT NOT NULL,  
    CorporationName NVARCHAR(20) NOT NULL
);  

CREATE TABLE ##corporationLinks
(  
    FromCorporationID INT NOT NULL,  
    ToCorporationID INT NOT NULL
);  

INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (1, 'Nike')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (2, 'Cocal Cola')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (3, 'Apple')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (4, 'Google')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (5, 'Amazon')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (6, 'Samsung')

INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (1, 2)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (2, 3)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (4, 5)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (4, 6)

SELECT * FROM ##corporationLinks WHERE FromCorporationID = 2 OR ToCorporationID = 2

/** 

Organisations (##corporationLinks) are...

Nike + Coca Cola + Apple + Marcy

and...

Google + Amazon + Samsung

**/

-- How do I eg get a list of companies where Coca Cola is in ... that is where FromCorporationID = 2 OR ToCorporationID = 2 ... result should be Nike, Coca Cola and Apple?
-- How do I eg get a list of companies where Samsung is in ... that is where FromCorporationID = 6 OR ToCorporationID = 6 ... result should be Google, Amazon Cola and Samsung?

DROP TABLE ##corporationLinks 
DROP TABLE ##corporations

UDPATE:

如果我需要找到可口可乐公司的一部分,那我会做些麻烦的事情...

SELECT * FROM ## corporation链接WHERE FromCorporationID = 2或ToCorporationID = 2

然后我将得到2个结果...

FromCorporationID   ToCorporationID
-----------------------------------
1                   2
2                   3

在此之后,我需要调查结果属于哪些公司...

SELECT * FROM ##corporationLinks WHERE FromCorporationID = 1 OR ToCorporationID = 1

SELECT * FROM ##corporationLinks WHERE FromCorporationID = 3 OR ToCorporationID = 3

然后我将再获得一家公司(7):

FromCorporationID   ToCorporationID
-----------------------------------
3                   7

然后我需要深入研究与7相关的公司...

SELECT * FROM ##corporationLinks WHERE FromCorporationID = 7 OR ToCorporationID = 7

也要深入研究该结果(猜测它称为递归)。等

更新2:

我已经更新了上面的示例,添加了另外一家公司,如果搜索的是可口可乐,该公司应返回。

上述查询(可口可乐)的预期结果:

CorporationID:
--------------
2
1
3
7

3 个答案:

答案 0 :(得分:0)

左加入## companies,包括其CorporationID,或者您可以在查询中包括公司名称,然后按CorporationID分组,您应该能够看到它属于哪个公司

答案 1 :(得分:0)

使用递归CTE

with 
  root as (select CorporationID id from companies where CorporationName = 'Coca Cola'),
  cte as (
    select 
      case when r.id = cl.FromCorporationID then cl.ToCorporationID else cl.FromCorporationID end id,
      case when r.id = cl.FromCorporationID then cl.FromCorporationID else cl.ToCorporationID end otherid      
    from corporationLinks cl inner join root r
    on r.id in (cl.FromCorporationID, cl.ToCorporationID)
    union all
    select 
      case when c.id = cl.FromCorporationID then cl.ToCorporationID else cl.FromCorporationID end id,
      case when c.id = cl.FromCorporationID then cl.FromCorporationID else cl.ToCorporationID end otherid            
    from corporationLinks cl inner join cte c
    on c.id in (cl.FromCorporationID, cl.ToCorporationID)
    and c.otherid not in (cl.FromCorporationID, cl.ToCorporationID)
  )
select id CorporationID from root
union all
select id from cte

请参见demo
结果:

> | CorporationID |
> | ------------: |
> |             2 |
> |             1 |
> |             3 |
> |             7 |

答案 2 :(得分:0)

只需使用递归CTE

WITH cte
AS
(
    SELECT l1.FromCorporationID,l1.ToCorporationID
    FROM ##corporationLinks l1
    UNION ALL
    SELECT cte.FromCorporationID,l3.ToCorporationID
    FROM ##corporationLinks l3
    JOIN cte 
        ON cte.ToCorporationID = l3.FromCorporationID

)

SELECT cte.ToCorporationID--parent
FROM cte
WHERE cte.FromCorporationID =2
UNION
SELECT cte.FromCorporationID--child
FROM cte
WHERE cte.ToCorporationID =2
UNION
SELECT c.CorporationID--self
FROM ##corporations c
WHERE c.CorporationID = 2