SAP HANA SQL Query to find all possible combinations between two columns

廉价感情. 提交于 2021-01-07 01:36:37

问题


The target is to create all possible combinations of joining the two columns using SAP HANA SQL. every article of the first column ('100','101','102','103') must be in the combination result.

Sample Code

create table basis
(article Integer,
supplier VarChar(10) );
Insert into basis Values (100, 'A');
Insert into basis Values (101, 'A');
Insert into basis Values (101, 'B');
Insert into basis Values (101, 'C');
Insert into basis Values (102, 'D');
Insert into basis Values (103, 'B');

Result set

combination_nr;article;supplier
1;100;'A'
1;101;'A'
1;102;'D'
1;103;'B'
2;100;'A'
2;101;'B'
2;102;'D'
2;103;'B'
3;100;'A'
3;101;'C'
3;102;'D'
3;103;'B'

Let suppose if we add one more row against 102 as 'A' then our result set will be like this

Also according to the below-given calculations now we have 24 result sets

1;100;'A'
1;101;'A'
1;102;'A'
1;103;'B'

2;100;'A'
2;101;'A'
2;102;'D'
2;103;'B'

3;100;'A'
3;101;'B'
3;102;'A'
3;103;'B'

4;100;'A'
4;101;'B'
4;102;'D'
4;103;'B'

5;100;'A'
5;101;'C'
5;102;'A'
5;103;'B'

6;100;'A'
6;101;'C'
6;102;'D'
6;103;'B'

Calculations:

article 100: 1 supplier ('A')
article 101: 3 suppliers ('A','B','C')
article 102: 1 supplier ('D')
article 103: 1 supplier ('B')
unique articles: 4 (100,101,102,103)
1x3x1x1 x 4 = 12 (combination rows)

来源:https://stackoverflow.com/questions/65528059/sap-hana-sql-query-to-find-all-possible-combinations-between-two-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!