Ruben
Ruben

Reputation: 1

postgresql crosstab weekday appointments for the week

I have a table with columns weekday, appointment, and appointment date for that week.

The table looks like this:

Weekday Appointment Date
Monday Doctor 2022-04-01
Tuesday Dentist 2022-04-02

And I would to display the table with Weekdays as columns:

Monday Tuesday ...
Doctor Dentist ...

So far my query looks like this:

select * from 
crosstab 
(
$$
select 
apt."name" as "Appointment"
, initcap(to_char(to_timestamp(apt."date"/1000), 'day')) as "Weekday"
from appointments as apt
where date_trunc('week', now()) <= to_timestamp(apt."date"/1000) 
and to_timestamp(apt."date"/1000) < date_trunc('week', now()) + '1 week'::interval 
$$,
$$
values ('Monday'::text), ('Tuesday'::text), ('Wednesday'::text), ('Thursday'::text), ('Friday'::text)
$$
)
as ct("Appointment" text, "Monday" text, "Tuesday"  text, "Wednesday" text, "Thursday"text, "Friday" text)

Upvotes: 0

Views: 27

Answers (0)

Related Questions