Nasty bug in Sybase Iq analytical function Last_value over partition

I discovered a nasty bug in the way Sybase Iq last_value over partition works. More precisely it returns random values. On the other hand first_value is working fine so for now I am using that one instead, with reverse ordering in partitions. But it makes my queries a little harder to understand.

Try this eg:

Let's say we have table TestLastValueTable

TableId     RequestId       Sender        MessageNumber      Location
1                    1                      alan                           2                     London
2                    1                      mary                          3                     Paris
3                    1                      mike                          1                     Sevilla
4                    2                      gaga                         2                      Paris
5                    2                      gugu                         3                      Sevilla

When using the following query:

Select Id, requestId , last_value(sender) over (partition by requestId order by messageNum desc)  as lastRequestSender ,
    first_value(location) over (partition by requestId order by messageNum desc)  as firstRequestLocation
From TestLastValueTable
order by Id

I expected the outcome to be

Id,    requestId,    lastRequestSender,    firstRequestLocation
1,          1,                   'mike',                             'Paris'
2,          1,                   'mike',                             'Paris'
3,          1,                   'mike',                             'Paris'
4,          2,                   'gaga',                             'Bucharest'
5,          2,                   'gaga',                             'Bucharest'

And it actually is :

Id,    requestId,    lastRequestSender,    firstRequestLocation
1,           1,                  'alan',                                   'Paris'
2,           1,                  'mary',                                  'Paris'
3,           1,                  'mike',                                  'Paris'
4,          2,                   'gaga',                                  'Bucharest'
5,          2,                   'gugu',                                  'Bucharest'
 

UPDATE: This bug is related to the partition, changing the partition to being specified by "rows between unbounded preceding and unbounded following" (which should not make any difference) , makes last_value to return the correct value:

Select Id, requestId , last_value(sender) over (partition by requestId order by messageNum desc rows between unbounded preceding and unbounded following)  as lastRequestSender ,
    first_value(location) over (partition by requestId order by messageNum desc)  as firstRequestLocation
From TestLastValueTable
order by Id

This article is part of the GWB Archives. Original Author: marocanu2001

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.