sql - Querying in Big Query? -


i have package table in big query follows:

 packageid  scanid  dispatchid  timestamp   status    p1         s1       null        t1        'in'    p2         s1       xxx         t2        'in'    p1         s2       yyy         t3        'pkin'    p1         s3       sss         t4        'iwi'    p1         s4       eee         t5        'lhp'    p2         s2       uuuu        t6        'uio'    p2         s3       null        t7        'jsk' 

i want retrieve following details:

packageid   latest-scanid   first-dispatch-time  last-dispatch-time   latest-status   p1            s4                 t3                 t5                 'lhp'  p2            s3                 t2                 t6                 'jsk'   

first-dispatch-time time when first time dispatch id appeared in package scan. last-dispatch-time time when last time dispatch id appeared in package scan.

is there way above table using big query or uer defined functions in big query?

one method uses windows functions , conditional aggregation:

select packageid,        max(case when seqnum = 1 dispatchid end) dispatchid,        min(case when dispatchid not null timestamp end) first_dispatchid,        max(case when dispatchid not null timestamp end) last_dispatchid,        max(case when seqnum = 1 status end) status (select t.*,              row_number() on (partition packageid order timestamp desc) seqnum       t      ) t group packageid; 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -