Следующий даст вам ожидаемый результат. Я использовал generate_series
, чтобы получить список дней и в результатах, которые я написал вручную, который можно автоматизировать с помощью procedure
.
SELECT *
FROM crosstab(
$$select (select distinct emp_id from test) as emp_id,t1.date1::text as date1,t2.w_hour from
(SELECT DATE('2019-12-31') - i date1 FROM generate_series(0, 30) i)t1
left join test t2 on t1.date1=t2.date1 order by 2$$
)
AS t("emp_id" integer,"2019-12-01" time,"2019-12-02" time,"2019-12-03" time,
"2019-12-04" time,"2019-12-05" time,"2019-12-06" time,"2019-12-07" time,
"2019-12-08" time,"2019-12-09" time,"2019-12-10" time,"2019-12-11" time,"2019-12-12" time,
"2019-12-13" time,"2019-12-14" time,"2019-12-15" time,"2019-12-16" time,"2019-12-17" time,
"2019-12-18" time,"2019-12-19" time,"2019-12-20" time,"2019-12-21" time,
"2019-12-22" time,"2019-12-23" time,"2019-12-24" time,"2019-12-25" time,
"2019-12-26" time,"2019-12-27" time,"2019-12-28" time,"2019-12-29" time,
"2019-12-30" time,"2019-12-31" time
);