Search code examples
kdb

Issue selecting timespan values in KDB


I am facing an issue while selecting the following following timespan :

t:([] date:2#.z.d ; time: 10D21:28:47.425287000 10D12:18:23.287989000 )

date       time                 
--------------------------------
2018.03.15 10D21:28:47.425287000
2018.03.15 10D12:18:23.287989000

when i run the following query, i am not getting the second record back

select from t where time within (12:00;13:00)

I am expecting the 2nd record from the table :

date       time                
-------------------------------
2018.03.15 10D12:18:23.287989000

Solution

  • Is the 10 in the time value 10D12:18:23.287989000 intentional ?

    The reason behind the data not coming back is the time (type timespan ) is actually not the nano seconds since midnight ; as per the table it is 10 days plus nanos since midnight

    To select the data only on the basis of time :

    q)select  from t where (`time$(`date$0)+time) within (12:00;13:00)
    date       time                
    -------------------------------
    2018.03.15 10D12:18:23.287989000
    

    Try adding the date and time from the table , you would see the date forwarded by 10 days

    q)select date+time from t
    
    date                         
    -----------------------------
    2018.03.25D21:28:47.425287000
    2018.03.25D12:18:23.287989000
    

    The timespan is basically nDhh:mm:ss.sssssssss , where n is relative to midnight. If its 0 then it's current day otherwise its +/- n days (depending on whether n is positive or negative).

    try running the following , it will return you the difference between the 2 timestamps as a timespan with n=10.

    q)2018.03.25D10:12:00.000000000 - 2018.03.15D10:00:00.000000000   
    10D00:12:00.000000000