create table table1 (
column1 INT,
column2 INT,
column3 INT);
INSERT INTO table1 VALUES ('333', '222', '111');
INSERT INTO table1 VALUES ('111', '222', '333');
INSERT INTO table1 VALUES ('222', '111', '333');
INSERT INTO table1 VALUES ('111', '222', '333');
INSERT INTO table1 VALUES ('222', '222', '222');
with concat_union as (
SELECT CONCAT(column1, column2, column3) as COMB, 'ORIG' as TYPE from table1
where CONCAT(column1, column2, column3) = '111222333'
UNION ALL
SELECT CONCAT(column1, column3, column2) as COMB, 'GEN' as TYPE from table1
UNION ALL
SELECT CONCAT(column2, column1, column3) as COMB, 'GEN' as TYPE from table1
UNION ALL
SELECT CONCAT(column2, column3, column1) as COMB, 'GEN' as TYPE from table1
UNION ALL
SELECT CONCAT(column3, column1, column2) as COMB, 'GEN' as TYPE from table1
UNION ALL
SELECT CONCAT(column3, column2, column1) as COMB, 'GEN' as TYPE from table1
)
select count(COMB) from concat_union where COMB = (select distinct COMB from concat_union where TYPE = 'ORIG');