Repetitions and combinations of values

Asked

Viewed 67 times

2

I have a table (corner) with 5 fields, each field can have the value from 1 to 80.

I wonder if it is possible in SQL or only in Delphi, to catch the dukes (combinations of 2 numbers) between 1 and 80 in the fields from C1 to C5. no matter where the values are on the line.

Example: combination of 01 and 02: 20 times.

(But 01 can be on C1, C2 or C3, and 02 can be on C4 or C5. The same number cannot be repeated on the same line).

1 answer

3

--aligning

select c1 c from tabela
union all
select c2 c from tabela
union all
select c3 c from tabela
union all
select c4 c from tabela
union all
select c5 c from tabela

--reading as a table

select virtual1.*
from
(
select c1 c from tabela
union all
select c2 c from tabela
union all
select c3 c from tabela
union all
select c4 c from tabela
union all
select c5 c from tabela
) virtual1 

--forcing a Cartesian

select virtual1.*,virtual2.*
from
(
select c1 c from tabela
union all
select c2 c from tabela
union all
select c3 c from tabela
union all
select c4 c from tabela
union all
select c5 c from tabela
) virtual1,
(
select c1 c from tabela
union all
select c2 c from tabela
union all
select c3 c from tabela
union all
select c4 c from tabela
union all
select c5 c from tabela
) virtual2
where virtual1.c <. virtual2.c

The basic idea is to make an "unpivot" of the table and then force a Cartesian.

  • Wow, I’ll try, and I’ll send a reply

  • v1 and v2 would be the correct values? then I would put 1., 2.? because it is giving syntax error from the first select select v1., v2.

  • It is the "alias" of the virtual table, I mounted with a name and called by another , corrected ...

  • Unfortunately it didn’t work. I did it in Delhi, but it takes about 30 minutes to complete the readings.. because it combines the values X and Y in all fields and all lines and still makes the sum of the same.

  • What went wrong ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.