0

I apologize if I'm missing something really obvious here, but hopefully you'll humour me!

I have these models

Employee - with id, first_name, last_name
Shift Type - with id, shift_name
Date Indices - with id, date
Locations - with id, location
Allocated shifts - with employee_id, shift_type_id, date_index_id, location_id

Now I can write queries that show me allocated shifts and join with locations, names etc. but what I was is to be able to produce a table that takes dates as columns and employees as rows to produce a roster like such

______________________________________________

|employee|date 1     |date 2     | date 3    |
|'dave'  |early shift|late shift |day off    |
|'martha'|day off    |early shift|early shift|

etc.

I'm sure I'm just pretty dumb, but how can I create these 'virtual' columns and link them to the employee?

muttonlamb
  • 6,341
  • 3
  • 26
  • 35

1 Answers1

2

You are looking for a "pivot" or "crosstab" query. Postgres has the additional module tablefunc for that. More info in this related answer:
PostgreSQL Crosstab Query

And many links to similar questions on SO from there.

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228