本文介绍了SAP HANA SQL查询以查找两列之间的所有可能组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是使用 SAP HANA SQL 创建连接两列的所有可能组合.第一列中的每个商品("100","101","102","103")都必须包含在组合结果中.

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.

示例代码

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');

结果集

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'

让我们假设,如果我们在 102上再添加一行作为'A',那么我们的结果集将像这样

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

根据下面给出的计算,我们现在有24个结果集

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'

计算:

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)

推荐答案

怎么样:

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

这篇关于SAP HANA SQL查询以查找两列之间的所有可能组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:39