I’m quite new at SQL and databases in general, so please be patient with me.
I have searched for a solution to this for days now. I have tried all sorts of possibilities, nothing.
The table I use has, among others, the following date columns:
job1_start, job1_end, job2_start, job2_end, job3_start, job3_end
I have been able to calculate the amount of months for each job with:
SELECT
DATEDIFF('day' "table"."job1_start", "table"."job1_end") / 365.00 * 12 AS job1_months
DATEDIFF('day' "table"."job2_start", "table"."job2_end") / 365.00 * 12 AS job2_months
DATEDIFF('day' "table"."job3_start", "table"."job3_end") / 365.00 * 12 AS job3_months
FROM "table"
(I use the ~/ 365.00 * 12~ in order to get decimals. So e.g. 29 days is 0.96 months, not 0)
From job1_months, job2_months, job3_months I would like to calculate the average number of months the worker stays at a job. Not dividing the total by 3, since some workers might only ever have had 1 or 2 jobs. So, maybe using AVG because it ignores NULL values?
I would be very thankful if someone could help me with this.
I use HSQLDB.