如果列记录与使用SQL Server的另一个表记录匹配,如何显示列记录?

时间:2017-09-11 13:54:56

标签: sql sql-server stored-procedures

我按列搜索记录,如果它与列中的任何一个匹配,则必须在行中显示整个列记录。

这个问题与我之前的问题类似。但是有一些变化,我无法得到结果

这是我的表

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.ItemIDRelationData

中的任何一列相匹配,我想获得完整的父排

期待输出

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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,但在这里你必须添加(通过Union或其他)父母,以便也是出类拔萃的:

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

答案 2 :(得分:0)

我试图根据Tyron78的解决方案进行移植。基本上我完成了他的两个版本 - 而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)

第一个使用union和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

使用unpivot的第二个解决方案:

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

第二个预制件更好,更好。