2017-09-11 29 views
0

我正在按列搜索記錄,如果它與任何列的列匹配,它必須在行中顯示整列記錄。如何顯示列記錄是否與使用SQL Server的另一個表記錄匹配?

這個問題與我以前的問題類似。但有一些變化,我不能得到結果

這裏是我的表

RelationData

Parent child1 child2 child3 
------------------------------------ 
111  112  113  117 
111  222  223  224 
444  441  442  443 
333  331  332  334 
888  887  889  885 

#tempRecord

ItemID 
----- 
112 
443 
888 

我希望得到整個如果#tempRecord.ItemID與0中的任何一列匹配,則爲父行

期待輸出

ItemID 
------ 
111 
112 
113 
117 
222 
223 
224 
     //111 is the parent of two row. so it will show 2 rows records 
444 
441 
442 
443 
888 
887 
889 
885 

我試着用Mr.GordonLinoff

SELECT 
    v.child ItemID 
FROM 
    RelationData rd outer apply 
     (values (rd.ID),(rd.ChildID1), (rd.ChildID2), (rd.childID3)) v(child) 
INNER JOIN ItemTable Item on item.id = v.child 
LEFT JOIN #tempData tt on item.id = tt.Itemid 
WHERE 
    (SELECT Itemid FROM #tempData) IN (rd.ID, rd.ChildID1, rd.ChildID2, rd.childID3) 
    and v.child is not null 
GROUP BY v.child 
+0

你的查詢有一個叫'itemTable'的東西,但它不是你問題的一部分。基於結果看起來是多餘的。 –

+0

@GordonLinoff對不起,我將刪除。我試試另一個以及 –

回答

1

的幫助,我覺得這下面的查詢你想要這個版本:

SELECT v.child as ItemID 
FROM RelationData rd OUTER APPLY 
     (VALUES (rd.ID), (rd.ChildID1), (rd.ChildID2), (rd.childID3) 
     ) v(child) JOIN 
     #tempData tt 
     ON tt.Itemid IN (rd.ID, rd.ChildID1, rd.ChildID2, rd.childID3) 
WHERE v.child IS NOT NULL; 

如果你不想重複使用SELECT DISTINCT

+0

它顯示錯誤'ON item.id' ... –

+0

你可能會離開項目表。只需要RelationData和#tempData表 –

0

嗯...如果它總是隻有三個孩子的,是可以解決相當簡單:

DECLARE @tRelation TABLE(
    parent int 
,child1 int 
,child2 int 
,child3 int 
) 

INSERT INTO @tRelation VALUES 
(111  ,112  ,113  ,117) 
,(111  ,222  ,223  ,224) 
,(444  ,441  ,442  ,443) 
,(333  ,331  ,332  ,334) 
,(888  ,887  ,889  ,885) 

;WITH cte AS(
    SELECT parent, parent AS child 
    FROM @tRelation 
    UNION 
    SELECT parent, child1 AS child 
    FROM @tRelation 
    UNION 
    SELECT parent, child2 AS child 
    FROM @tRelation 
    UNION 
    SELECT parent, child3 AS child 
    FROM @tRelation 
) 
SELECT child 
    FROM cte 
    WHERE parent = 111 

另一種方法可能會使用UNPIVOT,但在這裏你必須添加(通過聯盟或其他)父母爲了也是outoutted:

SELECT parent, child 
    FROM 
    (SELECT parent, child1, child2, child3 
    FROM @tRelation) c 
    UNPIVOT 
    (child FOR childno in (child1, child2, child3) 
) AS chlds 
0

我試圖根據Tyron78的解決方案unpivot。基本上我完成了他的兩個版本 - 並且unpivot版本表現更好。好一個!

設置的測試數據:

DECLARE @tRelation TABLE(
    parent int 
,child1 int 
,child2 int 
,child3 int 
) 
declare @tempRecord table(ItemId int) 

INSERT INTO @tRelation VALUES 
(111  ,112  ,113  ,117) 
,(111  ,222  ,223  ,224) 
,(444  ,441  ,442  ,443) 
,(333  ,331  ,332  ,334) 
,(888  ,887  ,889  ,885) 

insert into @tempRecord(ItemId) 
values(112), (443), (888) 

與工會和CTE第一溶液:

;WITH cte(parent, child) AS(
    SELECT parent, parent AS child 
    FROM @tRelation 
    union 
    SELECT parent, child1 AS child 
    FROM @tRelation 
    UNION 
    SELECT parent, child2 AS child 
    FROM @tRelation 
    UNION 
    SELECT parent, child3 AS child 
    FROM @tRelation 
) 

SELECT  cteChild.child 
FROM  cte 
      inner join 
      @tempRecord tempRecord 
      on tempRecord.ItemId = cte.child 
      inner join 
      cte cteChild 
      on cteChild.parent = cte.parent 

與逆透視第二溶液:

SELECT  child 
FROM  (
       SELECT  tRelation.parent, 
          tRelationSameParent.child1, 
          tRelationSameParent.child2, 
          tRelationSameParent.child3 
       FROM  @tRelation tRelation 
          inner join 
          @tempRecord tempRecord 
          on tempRecord.ItemId in (
            tRelation.parent, 
            tRelation.child1, 
            tRelation.child2, 
            tRelation.child3) 
          inner join 
          @tRelation tRelationSameParent 
          on tRelationSameParent.parent = tRelation.parent 
      ) c 
      UNPIVOT 
      (child FOR childno in (parent, child1, child2, child3)) AS chlds 

德第二個預成型件更好,是更好的。

相關問題