简体   繁体   English

SQL查询产生所有组合

[英]SQL Query Produce all Combinations

I have searched and looked but can't quite get the SQL query to work as I need. 我已经搜索并查看了,但是无法完全按照我的需要运行SQL查询。 I have this data: 我有此数据:

10019201        GEE78316634770
10019201        SIE78308706478
10019202        GEE78316634773
10019202        SIE78308706477
10019202        SIE78308706499
10019202        SIE78308706900
10019501        SIE78308706917
10019501        GEE78316634930

I want to create the following output based on the fact that the first column match, the following output: 我想基于第一列匹配的事实创建以下输出,以下输出:

GEE78316634770     SIE78308706478
SIE78308706478     GEE78316634770
GEE78316634773     SIE78308706477
GEE78316634773     SIE78308706499
GEE78316634773     SIE78308706900
SIE78308706477     GEE78316634773
SIE78308706477     SIE78308706499
SIE78308706477     SIE78308706900
SIE78308706499     GEE78316634773
SIE78308706499     SIE78308706477
SIE78308706499     SIE78308706900
SIE78308706900     GEE78316634773
SIE78308706900     SIE78308706477
SIE78308706900     SIE78308706499
SIE78308706917     GEE78316634930
GEE78316634930     SIE78308706917

So the first two records with 10019201 are matched up in the results as 因此,结果为10019201的前两个记录将匹配为

GEE78316634770     SIE78308706478
SIE78308706478     GEE78316634770

the next set of records with 10019202 - there are four matches so the results are: 下一组记录为10019202的记录-有四个匹配项,因此结果为:

GEE78316634773     SIE78308706477
GEE78316634773     SIE78308706499
GEE78316634773     SIE78308706900
SIE78308706477     GEE78316634773
SIE78308706477     SIE78308706499
SIE78308706477     SIE78308706900
SIE78308706499     GEE78316634773
SIE78308706499     SIE78308706477
SIE78308706499     SIE78308706900
SIE78308706900     GEE78316634773
SIE78308706900     SIE78308706477
SIE78308706900     SIE78308706499

each one matched with the other 3. 彼此匹配3。

Can someone help me with the query to get the results I need? 有人可以帮助我查询以获得所需的结果吗?

Thanks! 谢谢! Leslie 莱斯利

Use a self-join: 使用自联接:

{using column names ID and Value just to show syntax} {使用列名IDValue只是为了显示语法}

SELECT 
    t1.Value Value1,
    t2.Value Value2
FROM table t1
INNER JOIN table t2 
    ON t1.ID = t2.ID
    AND t1.Value <> t2.Value 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM