[英]Complex query melting my brain! Rails and Postgres
如果我在這里遺漏了一些非常明顯的東西,我很抱歉,但希望你能讓我感到幽默!
我有這些模特
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|
等等
我確定我只是非常愚蠢,但我怎樣才能創建這些“虛擬”列並將它們鏈接到員工?
您正在尋找“樞軸”或“交叉表”查詢。 Postgres有額外的模塊tablefunc 。 有關答案的更多信息:
PostgreSQL交叉表查詢
還有很多關於SO的類似問題的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.