Adding bodymass support

This commit is contained in:
2025-10-21 11:21:57 +02:00
parent 090d869bad
commit 3a3d121511
5 changed files with 2604 additions and 4 deletions

View File

@@ -11,7 +11,18 @@ create table account (
id serial primary key,
name varchar not null,
login varchar not null,
birthdate date not null
birthdate date not null,
bodymass integer,
constraint fk_bodymass foreign key (bodymass) references bodymass(id)
);
create table bodymass (
id serial primary key,
account integer not null,
time timestamptz not null,
kilos numeric(4,1) not null,
constraint fk_account foreign key (account) references account(id),
constraint unique_account_time unique (account, time),
constraint time_not_infty check (time <> 'infinity'::timestamptz)
);
create table training (
id serial primary key,

26
db/readme.md Normal file
View File

@@ -0,0 +1,26 @@
## Joining to bodymass table by time
Data in the bodymass table can be transformed as (for account 1):
```sql
select kilos, time as start, (case when lead(time) over (order by time asc) is null then 'infinity'::timestamptz else lead(time) over (order by time asc) end) as end from bodymass where account=1;
```
Thereby giving a table with clear timespans. Joining can then be performed by a nested table:
```
with bm_report as (
select kilos, time as start,
(case when lead(time) over (order by time asc) is null
then 'infinity'::timestamptz
else lead(time) over (order by time asc) end) as end from bodymass
where account=1)
select
t.time, t.exercise, t.kilos, b.kilos as bodymass, round(100*t.kilos/b.kilos, 1) as ratio
from training t
inner join bm_report b on t.time >= b.start and t.time < b.end
where t.account=1 and exercise=1
order by 1;
```