netezza - SQL first_value and last_value -


this netezza, runs flavor of postgres

i'm using first_value , last_value window functions, giving different values , confusing me.

here's code i'm running:

select     a.emp_id     , a.sequence     , first_value(a.dep_id) over(partition a.emp_id order a.sequence) first_dept     , last_value(a.dep_id) over(partition a.emp_id order a.sequence) last_dept     , first_value(a.sequence) over(partition a.emp_id order a.sequence) first_seq     , last_value(a.sequence) over(partition a.emp_id order a.sequence) last_seq     tname     1=1     , a.event_id in (1, 2)     , a.emp_id=11111 

for emp_id = 1111 returns 2 records.

emp_id   sequence   first_dept  last_dept   first_seq   last_seq 90565520    1       200800      200800      1           1 90565520    10      200800      200932      1           10 

i'm confused why last_seq returning 1 instead of 10

in window functions order by used "running" calculations, first row first_value , last_value returns values first row , second row returns values 2 rows , on.

you can ensure in adding sum(a.sequence) on (partition a.emp_id order a.sequence). in example 1 , 11 (= 1+10).

so should remove order by over , add order a.dep_id, a.sequence @ end of whole query.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -