Count SQL repeats with INTERSECT and COUNT

Asked

Viewed 45 times

0

I have an SQL table with hundreds of data, and I would like to introduce an array, the same be compared with this table and it is informed how many repetitions occurred. (Table --> EVENT | DADOS1 | DADOS2 | DADOS3 | DADOS4 | DADOS5 | DADOS6 | DADOS7 |DADOS8)

To be able to compare 2 events of this table to each other, but I want to compare 1 to the whole table and see the number of repetitions. For example, when comparing the 1247 event he will report that with 1 match (equal data in both, but all other different data) had 750 occurrences, 2 coincidences (data1 and data4 equal in both and different others - regardless of which data was equal to which, what matters and which had 2 coincidences) had 500 occurrences, 3 coincidences 470 occurrences, 4 coincidences 350 occurrences, up to 8 coincidences 2 occurrences.

In SQL compare 2 values would be:

SELECT COUNT(*) FROM
(
SELECT T.[DADOS1], T.[DADOS2], T.[DADOS3], T.[DADOS4], T.[DADOS5], T.[DADOS6], T.[DADOS7], T.[DADOS8] FROM [TABELA1] AS T WHERE [EVENTO] = 1
INTERSECT
SELECT T.[DADOS1], T.[DADOS2], T.[DADOS3], T.[DADOS4], T.[DADOS5], T.[DADOS6], T.[DADOS7], T.[DADOS8] FROM [TABELA1] AS T WHERE [EVENTO] = 3
) I
  • If you want : exact conciliation in all fields one to one field 1 x field 1 field 2 x field 2 etc.OR field 1 ratela against any field of table 2 ?

  • Field 1 ratela against any field in table 2.

1 answer

0

--crie duas views do tipo
create view v_tabela 1 as
select '1' ordem,campo1 campo from tabela1 union all
select '2' ordem,campo2 campo from tabela2 union all
...
/*idem tabela2*/

--localizando
select campo from v_tabela1 
intersect
select campo from v_tabela2

--listando

select * from v_tabela1
where campo in (select campo
                from (select campo from v_tabela1 
                      intersect
                      select campo from v_tabela2))

Browser other questions tagged

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