如何将每天的值限制为1000

时间:2019-01-08 14:12:05

标签: sql google-bigquery tableau

我有一个查询:

select A.*, A.DocumentID.DocId, D.Key, D.Value
from `moonoia-bpo-run.dam.events` A  
left join unnest (A.metadata) D 
where A.Creationtimestamp > '2018-10-01' 
order by Creationtimestamp desc 
limit 10000

我想将每天的值限制为10000。我该怎么办?

1 个答案:

答案 0 :(得分:2)

您可以使用row_number()来枚举行:

select *
from (select e.*, e.DocumentID.DocId, D.Key, D.Value,
             row_number() over (partition by date(a.creationtimestamp) order by rand()) as seqnum
      from `moonoia-bpo-run.dam.events` e left join
           unnest (A.metadata) D 
      where A.Creationtimestamp > '2018-10-01' 
     ) e
where seqnum <= 1000
order by Creationtimestamp desc