SQL 内部列之间的分组依据和相交

SQL Group by and intersect between internal columns

我有以下Table结构

Source| Category         |Tag1Value     | Tag2Value
1-q1  | Engagement Type  | Micro Audit  |Audit
1-q1  | Engagement Type  | Micro Audit  |Micro Audit
1-q1  | Industry         | Insurance    |Insurance

我想为每个类别确定 Tag2Values 中的所有 Tag1Value s 是否有任何基于集合的方法通过对结果进行分组和相交请帮忙。有一种迭代方法,但我正在寻找性能更高的解决方案,因为数据以百万计。

SELECT Category, Tag1Value
FROM table_name t1
WHERE EXISTS (SELECT 1 FROM table_name WHERE Tag2Value = t1.Tag1Value)

UPDATE

试试这个:

SELECT res.Category, res.tag, COUNT(res.tag) 
FROM
    (SELECT DISTINCT Category, Tag1Value tag
    FROM table_name
    UNION ALL
    SELECT DISTINCT Category, Tag2Value tag
    FROM table_name) res
GROUP BY res.Category, res.tag
HAVING COUNT(res.tag)>1

它return :

category          |    tag
-----------------------------------
Industry          |    Insurance
Engagement Type   |    Micro Audit