0
有誰知道使用activerecord構建數據透視表的方法,這將遠程DB中立?我試圖避免使用find_by_sql和數據庫特定查詢,但對於數據透視表或交叉表查詢,我不知道如何以不特定說MySQL的方式來完成此操作。 IE mySQL find_by_sql在postgresql數據庫中斷開。rails db中立數據透視表或交叉表
我發現http://crosstab.rubyforge.org/這個晦澀的crosstab創業板可能工作,但我想知道如果任何人有更好的解決方案。
例荒謬的東西像這樣的,基本上只是翻轉桌子上軸:
SELECT availables.name, rooms.id,
MAX(IF(to_days(availables.bookdate) - to_days('2009-06-13') = 0, availables.price, '')) AS day1,
MAX(IF(to_days(availables.bookdate) - to_days('2009-06-13') = 1, availables.price, '')) AS day2,
MAX(IF(to_days(availables.bookdate) - to_days('2009-06-13') = 2, availables.price, '')) AS day3,
MAX(IF(to_days(availables.bookdate) - to_days('2009-06-13') = 3, availables.price, '')) AS day4,
MAX(IF(to_days(availables.bookdate) - to_days('2009-06-13') = 4, availables.price, '')) AS day5,
MAX(IF(to_days(availables.bookdate) - to_days('2009-06-13') = 5, availables.price, '')) AS day6,
MAX(IF(to_days(availables.bookdate) - to_days('2009-06-13') = 6, availables.price, '')) AS day7,
AVG(availables.price),SUM(availables.price)
FROM `availables`
INNER JOIN rooms
ON availables.room_id=rooms.id
WHERE availables.room_id = '18382'
GROUP BY availables.name
還發現HTTP://api.rubyreports .ORG / – holden 2009-11-24 08:02:11