I'm trying to merge a scalar array json field within a group by to have all the distinct values in one list.
Consider the following table:
CREATE TABLE transaction
(
id INT UNSIGNED AUTO_INCREMENT,
source_account_id VARCHAR(32) NOT NULL,
target_account_ids JSON NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB CHARSET utf8mb4;
source_account_ids is a simple array of strings for example '["account1", "account2"]'.
I'd like to gather all the target_account_ids of a single source to have a unified result.
For example:
| id | source_account_id | target_account_ids |
|---|---|---|
| 1. | account1 | '["account1", "account2"]' |
| 2. | account1 | '["account1", "account3"]' |
And the desired result set would be:
| source_account_id | target_account_ids |
|---|---|
| account1 | '["account1", "account2", "account3"]' |
I tried to play around with JSON_ARRAYAGG but it just adds the arrays within another array and basically results in an "endless" array.