简体   繁体   中英

BigQuery/SQL: Select first row of each group

I am trying to select the first row of each group. Eg: below table I would want to keep product for www/edf/ and cate for www/abc/.

I have tried multiple ways, eg: ROW_NUMBER() OVER(PARTITION BY [...]) but somehow not getting expected outputs. The challenge for me here is that category does not have numbers as values, otherwise I can filter it down using max or min.

SELECT landing_page,   ROW_NUMBER  OVER ( PARTITION BY landing_page ORDER BY Page_Type DESC) AS ROW_NUM
from `xxxx.TEST.draft`

However I got this error: OVER keyword must follow a function call

Appreciate any help!

Landing_page Page_type
www/edf/ product
www/edf/ home
www/abc/ cate
www/abc/ home

I believe you are looking for the function [FIRST_VALUE][1] ?

SELECT 
    landing_page,   
    FIRST_VALUE(URL)  
        OVER ( PARTITION BY landing_page ORDER BY Page_Type DESC) AS first_url
FROM `xxxx.TEST.draft`

I answered my own question, sharing the code in case anyone needs:

SELECT session_id,landing_page, Page_Type, ROW_NUMBER ()  OVER (PARTITION BY session_id,landing_page ORDER BY Page_Type DESC) AS ROW_NUM
from `xxx._TEST.draft`
order by session_id desc 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM