简体   繁体   English

SAP HANA SQL 查询以查找两列之间的所有可能组合

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

The target is to create all possible combinations of joining the two columns using SAP HANA SQL .目标是使用SAP HANA SQL创建连接两列的所有可能组合。 every article of the first column ('100','101','102','103') must be in the combination result.第一列('100','101','102','103')的每篇文章都必须在组合结果中。

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假设如果我们在102 中再添加一行作为“A”,那么我们的结果集将是这样的

Also according to the below-given calculations now we have 24 result sets同样根据下面给出的计算,现在我们有 24 个结果集

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)

How about:怎么样:

select article, 
       count(supplier) as nb_supplier,
       STRING_AGG(supplier,',') as list_suppliers
from (select distinct article, supplier from basis)
group by article

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

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