Note: Question was edited to include the tables and show what exactly is expected as a result.
Lets say we have an SQL Table generated by:
CREATE TABLE T1 (
`a` INTEGER,
`b` DATETIME,
`c` VARCHAR(5)
);
INSERT INTO T1
(`a`, `b`, `c`)
VALUES
('5678', '2008-01-01 12:00', '12.34'),
('5678', '2008-01-01 12:01', NULL),
('5678', '2008-01-01 12:02', NULL),
('5678', '2008-01-01 12:03', '23.45'),
('5678', '2008-01-01 12:04', NULL);
What I need to execute is
SELECT * FROM(
SELECT a, b, c from T1
)AS Q GROUP BY c ORDER BY a, b;
Which gives:
5678 2008-01-01 12:00:00 12.34
5678 2008-01-01 12:01:00 NULL
5678 2008-01-01 12:03:00 23.45
What H2 suggests (and accepts) is
SELECT * FROM(
SELECT a, b, c from T1
)AS Q GROUP BY a,b,c ORDER BY a, b, c;
Which gives
5678 2008-01-01 12:00:00 12.34
5678 2008-01-01 12:01:00 NULL
5678 2008-01-01 12:02:00 NULL
5678 2008-01-01 12:03:00 23.45
5678 2008-01-01 12:04:00 NULL
Per some of your suggestions, these are the queries and results.
Suggestion 1:
SELECT max(a) as a, max(b) as b, c
FROM (
SELECT a, b, c from T1
) AS Q
GROUP BY c
ORDER BY a, b;
Gives
5678 2008-01-01 12:00:00 12.34
5678 2008-01-01 12:03:00 23.45
5678 2008-01-01 12:04:00 NULL
Suggestion 2:
SELECT *
FROM (
SELECT a, b, c from T1
) AS Q
GROUP BY c, a, b
ORDER BY a, b;
Gives
5678 2008-01-01 12:00:00 12.34
5678 2008-01-01 12:01:00 NULL
5678 2008-01-01 12:02:00 NULL
5678 2008-01-01 12:03:00 23.45
5678 2008-01-01 12:04:00 NULL
======================================
I need to obtain the results executed by the first query.
How should I properly change the syntax to accommodate the H2 demands while getting a desired result ?