How to group by / Order by with logical sequence
For PostgreSQL:
SELECT section,
ARRAY_AGG("number" ORDER BY "number") AS "numbers",
CONCAT_WS(' ', 'from', MIN("number"), 'to', MAX("number")) AS "sequence"
FROM (
SELECT *,
SUM(incr) OVER(ORDER BY section, "Condition", "number") AS grp
FROM (
SELECT *,
CASE WHEN LAG("Condition") OVER(ORDER BY section, "number") = "Condition"
THEN NULL
ELSE 1
END AS incr
FROM <your_table>
) q
WHERE NOT "Condition" = 'No'
) q
GROUP BY
section, grp
ORDER BY
section
;
section | numbers | sequence
---------+---------+-------------
A | {1,2} | from 1 to 2
A | {4,5,6} | from 4 to 6
B | {1,2} | from 1 to 2
(3 rows)
Here,
- the
CASE
statement will assign and incrementor (incr = 1
) to rows whose"Condition"
is different from the previous row - the
SUM
creates a running sum to serve as group value (grp
)
I give you part of the answer with some thoughts, the path to adopt according to me.
First, you need to create a new field that you can use to group your values.
Without this field, it's complicated to express your need:
Aggregate me the numbers by section, but when you meet the condition "No" you aggregate in another group.
You must arrive at the following result:
Secondly, you can execute the following sql query using the method array_agg
to aggregate your values in an array.
SELECT section, array_agg(number) as number -- use the array_agg method
FROM test -- name of your layer
WHERE "group" != '0' -- exclusion of the no condition
GROUP BY section, "group" -- group by with the field previously created
ORDER BY section, number
The result is the following :
Not exactly what you asked for, but if you can live with the section being only one row, sqlite has a group_concat function making it very simple:
select section, group_concat(number) numbers
from mytest
where condition <> 'No'
group by section
order by number