Skip to content

Instantly share code, notes, and snippets.

@Astyk
Last active February 11, 2019 14:10
Show Gist options
  • Save Astyk/91518ba7f1b177ad4ba8ea09b021c453 to your computer and use it in GitHub Desktop.
Save Astyk/91518ba7f1b177ad4ba8ea09b021c453 to your computer and use it in GitHub Desktop.
JSON_ARRAYAGG
DELIMITER //
DROP FUNCTION IF EXISTS JSON_ARRAYAGG//
CREATE AGGREGATE FUNCTION IF NOT EXISTS JSON_ARRAYAGG(next_value TEXT) RETURNS TEXT
BEGIN
DECLARE json TEXT DEFAULT '[""]';
DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN json_remove(json, '$[0]');
LOOP
FETCH GROUP NEXT ROW;
SET json = json_array_append(json, '$', next_value);
END LOOP;
END //
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment