【问题标题】:I have a table fetching values from an SP. I'd like to know if a combination of data is reversely repeated我有一个从 SP 获取值的表。我想知道数据组合是否反向重复
【发布时间】:2020-11-06 01:58:18
【问题描述】:

我有一个名为#hierachy 的表,其中包含三列: childId、parentId 和 linkType

数据如下:

childID  parentID  linktype
30  31          53
31  42          56
31  415349      18
31  437327      18
31  438333      18
35  32          56
32  35          18
32  38          52
32  39          52
32  439395      51
34  40          51

如果前两列 childID 和 parentID 之间存在反向重复,我想发现任何反向重复并返回“true”或“1” 例如,第六行和第七行是反向相似的:一个是 35 然后是 32,另一个是相反的:32 和 35。

谢谢!

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    你可以使用exists:

    select t.*,
           (case when exists (select 1
                              from t t2
                              where t2.childID = t.parentID and t2.parentID = t.childID
                             )
                 then 1 else 0
            end) as is_reversed
    from t;
    

    如果数据是从存储过程返回的,则需要将此逻辑放入存储过程中,或者将数据放入表中并使用该表运行逻辑。

    【讨论】:

      猜你喜欢
      • 2018-05-12
      • 1970-01-01
      • 2011-11-23
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2018-08-09
      • 1970-01-01
      相关资源
      最近更新 更多