SQL case statement - Kdb+ / qStudio

Home Forums Kdb+ / qStudio SQL case statement

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #346

    Sahid
    Guest

    Hello,
    Is there a SQL case statement in kdb?
    Thanks.

    #352

    admin
    Keymaster

    Kdb doesn’t have a “case when then else”, it has something similar called vector conditional.
    This link gives details: http://www.timestored.com/kdb-guides/q-quirks#caseStatement

    Basically standard SQL:
    `SELECT
    CASE
    WHEN Date1 >= Date2 THEN Date1
    ELSE Date2
    END AS MostRecentDate`

    Kdb Code:

    `q)update mostRecentDate:?[date1>=date2;date1;date2] from t
    date1 date2 mostRecentDate
    ————————————
    2001.08.05 2007.09.28 2007.09.28
    2011.05.21 2007.07.25 2011.05.21
    2005.08.10 2008.07.06 2008.07.06
    2003.02.16 2008.10.11 2008.10.11
    2007.10.05 2004.03.01 2007.10.05`

    The vector conditional takes three args ?[a;b;c]
    a is a list of booleans
    b and c are a list of same typed values
    where a was true, the value is taken from b, otherwise it’s taken from c.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘SQL case statement’ is closed to new replies.