具有复杂查询的预制列上的 sql sum 或聚合函数

发布于 2024-08-13 08:29:07 字数 1212 浏览 8 评论 0原文

我有一个查询,需要在很长一段时间内获取一周 7 天的平均数据(价格)。周一、周二等的 IE 平均价格。它工作正常,但我不确定如何在同一个查询中求和该查询找到的平均价格?求和第 1 天..第 5 天

按照目前的情况,此查询对所有价格的全部求和...即巨大的数字..不是来自平均值。

有什么想法吗?顺便说一句,这是来自 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

I have a query that takes avg data (prices) from 7 days of the week for a long interval. IE avg prices for monday, tues, etc. It works fine, but I'm unsure how I can in the same query sum the avgs that this query finds? Summing Day1..Day5

As it stands this query sums the entire from of all the prices... IE huge number.. not from the avg.

Any ideas? BTW this is from Postgres... Thanks

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夢归不見 2024-08-20 08:29:07

这是应该有用的东西......

select avg(mon+tues+weds+thur+fri) as averageall,
       sum(mon+tues+weds+thur+fri) as sumall,
  avg(mon) as avgmon,
  avg(tues) as avgtues,
  avg(weds) as avgweds,
  avg(thurs) as avgthurs,
  agv(fri) as avgfri,
MAX((SIGN(spots)-1) + 1) AS beds
from
(
SELECT rooms.name, rooms.id,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 1 THEN (availables.price) ELSE 0 END) AS mon,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 2 THEN (availables.price) ELSE 0 END) AS tues,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 3 THEN (availables.price) ELSE 0 END) AS weds,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 4 THEN (availables.price) ELSE 0 END) AS thur,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 5 THEN (availables.price) ELSE 0 END) AS fri,
spots, rooms.id, rooms.name
 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
)

注意我没有测试,所以可能有拼写错误。

Here is something that should work....

select avg(mon+tues+weds+thur+fri) as averageall,
       sum(mon+tues+weds+thur+fri) as sumall,
  avg(mon) as avgmon,
  avg(tues) as avgtues,
  avg(weds) as avgweds,
  avg(thurs) as avgthurs,
  agv(fri) as avgfri,
MAX((SIGN(spots)-1) + 1) AS beds
from
(
SELECT rooms.name, rooms.id,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 1 THEN (availables.price) ELSE 0 END) AS mon,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 2 THEN (availables.price) ELSE 0 END) AS tues,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 3 THEN (availables.price) ELSE 0 END) AS weds,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 4 THEN (availables.price) ELSE 0 END) AS thur,
CASE WHEN EXTRACT(DOW FROM availables.bookdate) = 5 THEN (availables.price) ELSE 0 END) AS fri,
spots, rooms.id, rooms.name
 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
)

note I did not test so there might be typos.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文