SQL permutation and combination




1. Overview of permutations and combinations


Permutation and combination are commonly used data organization methods for discrete data. This section will introduce the SQL implementation methods of permutation and combination respectively, and focus on the processing of data through combination with examples.

How to use SQL to implement permutations and combinations? This section will abstract general solutions by introducing group comparison statistical scenarios.

This article attempts to find a unique approach, emphasizing that through flexible and divergent data processing thinking, complex data scenarios can be solved using the most basic syntax. The SQL statements involved in the article use the basic syntax and some advanced syntax features of Hive SQL.

Related functions refer to MaxCompute:https://help.aliyun.com/zh/maxcompute/user-guide/overview/?spm=a2c4g.11186623.0.0.738048b99fbTqb

2. SQL permutation and combination

2.1. Arrangement


The definition of arrangement: from n different elements, any m (m<=n, m and n are both natural numbers) elements are arranged in a column in a certain order, which is called taking out from n different elements A permutation of m elements; the number of all permutations in which m elements are taken from n different elements is called the number of permutations in which m elements are taken from n different elements, with the symbol A n m A_n^m Anmexpress

Insert image description here

For example, for the character sequence['A', 'B', 'C'], 2 characters can be repeatedly selected from the sequence each time. How to obtain all the permutations?

The SQL implementation of the arrangement is as follows:

select 
    val1, val2, concat(val1, val2) as perm
from (select array('A', 'B', 'C') 

Guess you like

Origin blog.csdn.net/weixin_55629186/article/details/135000998
Recommended