I'm trying to remove the duplicate combinations from my cross join select statement, I have followed this example but the duplicated combination is still there, it even mixes the credit_id
and loan_id
. How am I supposed to do this?
My code
SELECT DISTINCT
CASE WHEN `credit`.`id`<=`loan`.`id` THEN `credit`.`id` ELSE `loan`.`id` END AS `credit_id`,
CASE WHEN `credit`.`id`<=`loan`.`id` THEN `loan`.`id` ELSE `credit`.`id` END AS `loan_id`
FROM
(SELECT `id`,`city` as `City with credit`,`fishing_segment` FROM `data`
WHERE `city` LIKE '%Credit') AS `credit`
CROSS JOIN
(SELECT `id`,`city` as `City with loan`,`fishing_segment` FROM `data`
WHERE `city` LIKE 'Loan%') AS `loan`;
The result
+-----------+---------+
| credit_id | loan_id |
+-----------+---------+
| 1 | 2 |
| 2 | 3 |
| 2 | 5 |
| 1 | 4 |
| 3 | 4 |
| 4 | 5 |
| 1 | 6 |
| 3 | 6 |
| 5 | 6 |
+-----------+---------+
The result that I want
+-----------+---------+
| credit_id | loan_id |
+-----------+---------+
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
+-----------+---------+
Here's the FIDDLE