title: How should I design the schema to get the last 2 records of each clustering key in Cassandra?
body: <p>Each row in my table has 4 values <code>product_id</code>, <code>user_id</code>, <code>updated_at</code>, <code>rating</code>.</p>
  
  <p>I'd like to create a table to find out how many users changed <code>rating</code> during a given period.</p>
  
  <p>Currently my schema looks like:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS ratings_by_product (
    product_id  int,
    updated_at timestamp,
    user_id int,
    rating int,
    PRIMARY KEY ((product_id  ), updated_at , user_id ))
  WITH CLUSTERING ORDER BY (updated_at DESC, user_id ASC);
  </code></pre>
  
  <p>but I couldn't figure out the way to only get the last 2 rows of each user in a given time window.</p>
  
  <p>Any advice on query or changing the schema would be appreciated.</p>