akretschmer
Datenbank-Guru
- Beiträge
- 10.371
mal so als Fingerübung...
Du erkennst den Weg?
Code:
edb=*# select * from kinder ;
name | geb_datum
-------+---------------------
max | 2014-02-01 00:00:00
susi | 2015-02-01 00:00:00
tilo | 2016-02-01 00:00:00
elli | 2017-02-01 00:00:00
sarah | 2018-02-01 00:00:00
fritz | 2019-02-01 00:00:00
(6 rows)
edb=*# select *, age(geb_datum) from kinder ;
name | geb_datum | age
-------+---------------------+------------------------
max | 2014-02-01 00:00:00 | 7 years 9 mons 18 days
susi | 2015-02-01 00:00:00 | 6 years 9 mons 18 days
tilo | 2016-02-01 00:00:00 | 5 years 9 mons 18 days
elli | 2017-02-01 00:00:00 | 4 years 9 mons 18 days
sarah | 2018-02-01 00:00:00 | 3 years 9 mons 18 days
fritz | 2019-02-01 00:00:00 | 2 years 9 mons 18 days
(6 rows)
edb=*# select *, extract(year from age(geb_datum)) as alter from kinder ;
name | geb_datum | alter
-------+---------------------+-------
max | 2014-02-01 00:00:00 | 7
susi | 2015-02-01 00:00:00 | 6
tilo | 2016-02-01 00:00:00 | 5
elli | 2017-02-01 00:00:00 | 4
sarah | 2018-02-01 00:00:00 | 3
fritz | 2019-02-01 00:00:00 | 2
(6 rows)
edb=*# with x as (select *, extract(year from age(geb_datum)) as alter from kinder) select case when alter between 1 and 4 then 'bis 4' else 'über 4' end as gruppe, string_agg(name,', ') from x group by 1;
gruppe | string_agg
--------+--------------------
bis 4 | elli, sarah, fritz
über 4 | max, susi, tilo
(2 rows)
Du erkennst den Weg?