Cassandra: get last with a non-null value in a column

Viewed 847

I have a Cassandra table where each column can contain a value or a NULL. But if it contains a NULL, I know that all the next values in that column are also NULL.

Something like this:

+------------+---------+---------+---------+
|       date | column1 | column2 | column3 |
+------------+---------+---------+---------+
| 2017-01-01 |       1 |     'a' |    NULL |
| 2017-01-02 |       2 |     'b' |    NULL |
| 2017-01-03 |       3 |    NULL |    NULL |
| 2017-01-04 |       4 |    NULL |    NULL |
| 2017-01-05 |    NULL |    NULL |    NULL |
+------------+---------+---------+---------+

I need a query that, for a given column, returns the date of the last column with a non-null value. In this case:

  • For column1, '2017-01-04'
  • For column2, '2017-01-02'
  • For column3, no result returned.

In SQL it would be something like this:

SELECT date
FROM my_table
WHERE column1 IS NOT NULL
ORDER BY date DESC LIMIT 1

Is it possible in any way, or should I break the table into one table for each column to avoid the NULL situation at all?

1 Answers
Related