我按日平均房价。我想找到每天最高费率的平均房产:
select Prop_Name as property, arrival_date as arrival, round(avg(Rate)) as rate
from [SomeDataBase]
where [Timestamp] >= '2015-06-15 00:00:00.000'
and [Timestamp] <= '2015-06-20 23:59:59.000'
group by property, arrival
order by arrival asc limit 10;
所以我将返回以下内容:
Property Date Rate
Prop_One 2015-06-15 00:00:00 UTC 281.0
Prop_Two 2015-06-15 00:00:00 UTC 343.0
Prop_Three 2015-06-15 00:00:00 UTC 266.0
Prop_One 2015-06-15 00:00:00 UTC 87.0
Prop_Three 2015-06-15 00:00:00 UTC 132.0
Prop_Two 2015-06-15 00:00:00 UTC 80.0
我想在没有一天重复的情况下返回每天的最高费率:
Prop_Three 2015-06-15 00:00:00 UTC 400.0
Prop_One 2015-06-16 00:00:00 UTC 586.0
Prop_Three 2015-06-17 00:00:00 UTC 190.0
Prop_Two 2015-06-18 00:00:00 UTC 180.0
答案 0 :(得分:2)
我认为bigquery支持row_number()
,所以我认为这样可行:
select t.*
from (select Prop_Name as property, arrival_date as arrival,
round(avg(Rate)) as rate,
row_number() over (partition by arrival_date order by avg(rate) desc) as seqnum
from [SomeDataBase]
where [Timestamp] >= '2015-06-15' and
[Timestamp] < '2015-06-21'
group by property, arrival
) t
where seqnum = 1;
编辑:
Arrgh。以上是标准SQL,但以下内容可能适用于Bigquery:
select t.*
from (select t.*,
row_number() over (partition by arrival_date order by rate desc) as seqnum
from (select Prop_Name as property, arrival_date as arrival,
round(avg(Rate)) as rate
from [SomeDataBase]
where [Timestamp] >= '2015-06-15' and
[Timestamp] < '2015-06-21'
group by property, arrival
) t
) t
where seqnum = 1;