How to get the mapping relationship between two columns in a table

时间:2022-03-06 01:11:08

If a table have column A and B

Count(distinct A) as Da

Count(distinct B) as Db

Count(distinct A, B) as Dab

The Da/Db<=Dab, this will be always true;

1. if(Da=Db=Dab) => A:B as 1:1

2. if(Da<(Db=Dab)) => A:B as 1:N

3. if(Db<(Da=Dab)) => A:B as N:1

4. if(Da!=Db!=Dab) => A:B as N:N //relashionship is mess

For example for 4, how to generate the smaple data, first build a 1:N and then change the column to be N:N, like below
1    2

1    3

1    4

2    5

=>

1   2

1   3

1   4

2   4

Da=2, Db=3,Dab=4