0
我有一個查詢需要從一週的7天內獲取平均數據(價格)很長的時間間隔。 IE的平均價格爲星期一,tues等。它工作正常,但我不確定如何在同一個查詢中總結此查詢發現的平均值?總結Day1..Day5複雜查詢的預fab列上的sql總結或聚合函數
因爲它站立此查詢總和從所有的價格... IE瀏覽器巨大的數字..不是從平均。
任何想法?順便說一句,這是從Postgres的...謝謝
SELECT rooms.name, rooms.id,
MAX(CASE WHEN (EXTRACT(DOW FROM availables.bookdate) - EXTRACT(DOW FROM DATE '2009-12-13')) = 0 THEN (availables.price) ELSE 0 END) AS day1,
MAX(CASE WHEN (EXTRACT(DOW FROM availables.bookdate) - EXTRACT(DOW FROM DATE '2009-12-13')) = 1 THEN (availables.price) ELSE 0 END) AS day2,
MAX(CASE WHEN (EXTRACT(DOW FROM availables.bookdate) - EXTRACT(DOW FROM DATE '2009-12-13')) = 2 THEN (availables.price) ELSE 0 END) AS day3,
MAX(CASE WHEN (EXTRACT(DOW FROM availables.bookdate) - EXTRACT(DOW FROM DATE '2009-12-13')) = 3 THEN (availables.price) ELSE 0 END) AS day4,
MAX(CASE WHEN (EXTRACT(DOW FROM availables.bookdate) - EXTRACT(DOW FROM DATE '2009-12-13')) = 4 THEN (availables.price) ELSE 0 END) AS day5,
(AVG(availables.price)) AS avg,
(SUM(availables.price)) AS sum, MAX((SIGN(spots)-1) + 1) AS beds
FROM availables
INNER JOIN rooms
ON availables.room_id=rooms.id
WHERE availables.room_id = '1780' AND availables.price > 0
GROUP BY rooms.id, rooms.name