在 SQL 服务器中按位

Bitwise in SQL Server

我需要一些帮助,我的观点 return parent 和 child ID。

当parentID为N、M或SU类型时,视图returnID为自己

现在,我的问题是,当 child 是 C 类型时,我想 return ChildID = ParentID

示例:

  1. tabley.ID8838的类型是C
  2. tabley.ID8834的类型是M(他的parentID)
  3. tabley.ID8822 的类型是 SU

return 应该是:

 ChildID   ParentID
 ------------------
   8838      8834 
   8822      8822

我认为我的问题出在这部分代码中

SELECT type 
FROM tabletype 
WHERE val & 17 <> val)

因为我的 return 现在是 :

 ChildID   ParentID
 ------------------
   8838      8838 
   8822      8822

非常感谢。

代码:

CREATE OR ALTER  VIEW V1 (ChildID, ParentID) AS
With MyCTE (ChildID, ParentID) 
AS (    
--Anchor. Get the roots     
SELECT C_ID, C_ID as ParentID FROM tablex     
WHERE val1 is null
OR C_ID IN (SELECT C_ID FROM tabley WHERE TYPE IN (SELECT NAME FROM tabletype WHERE val& 17 <> 0)
UNION ALL     
--Recursive. Get the direct descendants of the "previous" case    
SELECT t.C_ID, M.ParentID     
FROM tablex t INNER JOIN MyCTE M on t.ID=v.val1 

WHERE t.C_ID NOT IN (SELECT ID FROM tabley) 
    OR ID IN (SELECT ID FROM tabley 
    WHERE TYPE IN (SELECT type FROM tabletype WHERE val & 17 <> val)) 
) SELECT ChildID, ParentID FROM MyCTE 

桌子:

 ID   type
8822  SU
8838  C
8834  M

表格类型:

TYPE val
 C    11
 D    4
 F    11
 M    27 
 N    1 
 S    11
 SU   16 

好的答案是编辑:

val& 17 <> 0 to SETUP & 11 = 0
val& 17 = 0 to SETUP & 11  <> 0
 

--

CREATE OR ALTER  VIEW V1 (ChildID, ParentID) AS
With MyCTE (ChildID, ParentID) 
AS (    
--Anchor. Get the roots     
SELECT C_ID, C_ID as ParentID FROM tablex     
WHERE val1 is null
OR C_ID IN (SELECT C_ID FROM tabley WHERE TYPE IN (SELECT NAME FROM tabletype WHERE val& 11 = 0)
UNION ALL     
--Recursive. Get the direct descendants of the "previous" case    
SELECT t.C_ID, M.ParentID     
FROM tablex t INNER JOIN MyCTE M on t.ID=v.val1 

WHERE t.C_ID NOT IN (SELECT ID FROM tabley) 
OR ID IN (SELECT ID FROM tabley 
WHERE TYPE IN (SELECT type FROM tabletype WHERE val & 11 <> val)) 
) SELECT ChildID, ParentID FROM MyCTE