0

如果我在这里遗漏了一些非常明显的东西,我深表歉意,但希望你能幽默我!

我有这些模型

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

现在我可以编写查询来显示分配的班次并加入位置、名称等,但我是能够生成一个表,该表将日期作为列,将员工作为行来生成这样的名册

______________________________________________

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

等等

我确定我很笨,但是我怎样才能创建这些“虚拟”列并将它们链接到员工?

4

1 回答 1

2

您正在寻找“枢轴”或“交叉表”查询。Postgres 有额外的模块tablefunc。此相关答案中的更多信息:
PostgreSQL Crosstab Query

以及从那里指向类似问题的许多链接。

于 2013-03-20T04:42:11.527 回答