This repository has been archived on 2024-01-15. You can view files and clone it, but cannot push or open issues or pull requests.
vn-mysql/udfs/test/multimax.sql

37 lines
971 B
SQL

DROP TEMPORARY TABLE IF EXISTS tData;
CREATE TEMPORARY TABLE tData
ENGINE = MEMORY
SELECT 1 `group`, 1 `max`, 10 `value`
UNION SELECT 1, 2, 20
UNION SELECT 1, 3, 30
UNION SELECT 1, 2, 21
UNION SELECT 1, NULL, -1
UNION SELECT 2, '4', 40
UNION SELECT 2, '6', 60
UNION SELECT 2, '5', 50
UNION SELECT 2, '5', 51
UNION SELECT 2, NULL, -1
UNION SELECT 3, 7, 70
UNION SELECT 3, 8, 80
UNION SELECT 3, 9, NULL
UNION SELECT 3, 8, 81
UNION SELECT 3, NULL, -1
UNION SELECT 4, NULL, 101
UNION SELECT 4, NULL, 102;
DROP TEMPORARY TABLE IF EXISTS tExpect;
CREATE TEMPORARY TABLE tExpect
ENGINE = MEMORY
SELECT 1 `group`, 30 expect
UNION SELECT 2, 60
UNION SELECT 3, NULL
UNION SELECT 4, 102;
SELECT e.`group`, r.result, e.expect, r.result <=> e.expect isRight
FROM tExpect e
LEFT JOIN (
SELECT `group`, multimax(`max`, `value`) result
FROM tData GROUP BY `group`
) r ON r.`group` = e.`group`;
DROP TEMPORARY TABLE tData, tExpect;