MySQL AVG和分组

时间:2012-12-19 23:11:15

标签: mysql group-by average

我正在努力处理一个MySQL语句,希望得到一些指导,因为我很接近,但并不完全存在。我有一个数据库,其中包含一个属性地址表和属性租赁列表。属性地址与一个或多个区域相关,这些区域与区域表相关,然后与一个郊区表相关。

我正在尝试创建一个结果,它给出了每月每个郊区的平均租金和卧室数。

例如:

District Suburb     Month YEAR  YMD              Bedrooms DataAverage
Nelson   The Brook  01    2012  2012-01-01 00:00 1        190
Nelson   The Brook  01    2012  2012-01-01 00:00 2        274
Nelson   The Brook  01    2012  2012-01-01 00:00 3        341

然后我可以将其转换为表格如下:

Average Rent
Beds by Suburb Jan-12 Feb-12 Mar-12 Apr-12 May-12 Jun-12 Jul-12 The Brook 1 $150 $245 $160 $285 $135 $370 $350 2 $330 $340 $380 $310 $335 $345 $355 3 $350 $380 $310 $395 $380 $350 $350 Inner City 1 $160 $245 $260 $285 $295 $300 $350 2 $360 $440 $480 $410 $535 $545 $555 3 $370 $480 $510 $595 $480 $450 $550

My Current SQL查询是这样的:

SELECT d.name as District, s.name AS Suburb,
FROM_UNIXTIME(l.StartDate,'%m') AS Month,
FROM_UNIXTIME(l.StartDate,'%Y') AS YEAR,
FROM_UNIXTIME(l.StartDate, '%Y-%m-01 00:00') AS YMD,
p.Bedrooms,
REPLACE(FORMAT(AVG(l.RentPerWeek),0),',','') AS DataAverage
FROM properties p
LEFT JOIN listings l on l.property_id=p.id
LEFT JOIN regions r on p.region_id=r.id
LEFT JOIN districts d on d.region_id=r.id
LEFT JOIN suburbs s on s.district_id=d.id
WHERE FROM_UNIXTIME(l.StartDate) BETWEEN DATE(NOW()) - INTERVAL (DAY(NOW()) - 1) DAY - INTERVAL 11 MONTH AND NOW()
GROUP BY District, Suburb, Year, Month, Bedrooms 
ORDER BY District, Suburb ASC, YMD ASC, Bedrooms ASC

不幸的是,我得到的结果与每个郊区的结果相同。我想我可能需要创建一个子查询SQL语句才能使其正常工作,但我并不完全确定。

所以我得到这样的东西:

District Suburb     Month YEAR  YMD              Bedrooms DataAverage
Nelson   The Brook  01    2012  2012-01-01 00:00 1        190
Nelson   The Brook  01    2012  2012-01-01 00:00 2        330
Nelson   The Brook  01    2012  2012-01-01 00:00 3        350
Nelson   The Brook  02    2012  2012-02-01 00:00 1        245
Nelson   The Brook  02    2012  2012-02-01 00:00 2        340
Nelson   The Brook  02    2012  2012-02-01 00:00 3        380
...
Nelson   Inner City 01    2012  2012-01-01 00:00 1        190
Nelson   Inner City 01    2012  2012-01-01 00:00 2        330
Nelson   Inner City 01    2012  2012-01-01 00:00 3        350
Nelson   Inner City 02    2012  2012-02-01 00:00 1        245
Nelson   Inner City 02    2012  2012-02-01 00:00 2        340
Nelson   Inner City 02    2012  2012-02-01 00:00 3        380
.etc.

Average Rent
Beds by Suburb Jan-12 Feb-12 Mar-12 Apr-12 May-12 Jun-12 Jul-12 The Brook 1 $150 $245 $160 $285 $135 $370 $350 2 $330 $340 $380 $310 $335 $345 $355 3 $350 $380 $310 $395 $380 $350 $350 Inner City 1 $150 $245 $160 $285 $135 $370 $350 2 $330 $340 $380 $310 $335 $345 $355 3 $350 $380 $310 $395 $380 $350 $350

非常感谢任何指示或协助。

1 个答案:

答案 0 :(得分:0)

假设id是每个表的主键,那么根据您的查询文本,属性区域相关联,凭借region_id表格上的properties列:

      FROM properties p
      LEFT
      JOIN regions r 
        ON p.region_id=r.id

区域相关联(可能是区域是区域的细分区域。)

      LEFT
      JOIN districts d
        ON d.region_id=r.id

郊区相关联(可能是郊区是区的一个分区。)

      LEFT
      JOIN suburbs s
        ON s.district_id=d.id

最终结果是 region 中的每个属性都与区域中的每个相关联,并与每个相关联>每个区内的郊区

因此,您将获得区域内所有房产的平均租金值。

要获得每个郊区的租金值,您确实需要属性与其郊区之间的关系。

您真正需要的是suburb_id表上的properties列作为suburbs表的外键。

      LEFT
      JOIN suburbs s 
        ON s.district_id=d.id
       AND s.id = p.suburb_id