如何在分区表中获取最小值

时间:2019-08-06 06:00:43

标签: sql google-bigquery

我有一个使用以下代码的示例表

WITH tab as(
 select * from UNNEST([STRUCT(timestamp('2019-05-11 18:04:15') as create_time, 101 as jobid, 'rkc' as user),
 (timestamp('2019-05-11 18:04:15'), 101 , 'ash'), (timestamp('2019-05-11 18:04:15'), 101,'adi'),

 (timestamp('2019-05-20 18:04:15'), 102 , 'rkc'),
 (timestamp('2019-05-20 18:04:15'), 102 , 'ash'), (timestamp('2019-05-20 18:04:15'), 102,'adi'),
 (timestamp('2019-05-20 18:04:15'), 102 , 'sri')

 ]))

 select * from tab

从上表中,我只想为每个用户获取最小的jobid和最小的create_time

我期望以下输出<code>enter image description here</code>

有人可以帮我吗

谢谢!!

5 个答案:

答案 0 :(得分:0)

您可以使用row_number()

select * from
(
select * , row_number() over(partition by user order by create_time) as rn
from tablename
)A where rn=1

答案 1 :(得分:0)

您可以使用group by和min()来实现。

select create_time, min(jobid), user from UNNEST group by user, create_time

答案 2 :(得分:0)

有多种方法可以实现这一目标

使用row_number()。您可以从此链接中找到有关row_number的更多信息。ROW_NUMBER

 with ct as
 ( select jobid, user, create_time, 
   row_number() over (partition by Jobid order by create_time ) as Slno 
   from yourtable
 )
 select * from ct where slno = 1

OR

您可以将聚合函数与group by一起使用。有关此的更多信息,您可以找到此链接。 GROUP BY

通过使用MIN

SELECT JOB_ID, USER, MIN(CREATE_DATE) AS CREATEDATE FROM YOURTABLE GROUP BY JOB_ID, USER

答案 3 :(得分:0)

您可以使用某些内置功能来执行此操作而无需子查询:

select as value ARRAY_AGG(tab ORDER BY create_time DESC LIMIT 1)[ordinal(1)]
from tab
group by jobid

答案 4 :(得分:0)

  

我只想为session = boto3.Session( aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, aws_session_token=SESSION_TOKEN, )

提取 minimum jobidminimum create_time

以下是用于BigQuery标准SQL

each user

如果要应用于问题的样本数据

#standardSQL
SELECT 
  MIN(create_time) create_time, 
  MIN(jobid) jobid, 
  user
FROM `project.dataset.table`
GROUP BY user

结果是

WITH `project.dataset.table` AS (
  SELECT * FROM UNNEST([STRUCT
    (TIMESTAMP('2019-05-11 18:04:15') AS create_time, 101 AS jobid, 'rkc' AS user),
    (TIMESTAMP('2019-05-11 18:04:15'), 101 , 'ash'), 
    (TIMESTAMP('2019-05-11 18:04:15'), 101,'adi'),
    (TIMESTAMP('2019-05-20 18:04:15'), 102 , 'rkc'),
    (TIMESTAMP('2019-05-20 18:04:15'), 102 , 'ash'), 
    (TIMESTAMP('2019-05-20 18:04:15'), 102,'adi'),
    (TIMESTAMP('2019-05-20 18:04:15'), 102 , 'sri')
]))