title: How to get ' COUNT DISTINCT' over moving window
body: <p>I'm working on a query to compute the distinct users of particular features of an app within a moving window. So, if there's a range from 15-20th October, I want a query to go from 8-15 Oct, 9-16 Oct etc and get the count of distinct users per feature. So for each date, it should have x rows where x is the number of features.</p>
  
  <p>I have a query the following query so far:</p>
  
  <pre><code>WITH V1(edate, code, total) AS
     (
       SELECT date, featurecode, 
      DENSE_RANK() OVER ( PARTITION BY (featurecode ORDER BY accountid ASC) + DENSE_RANK() OVER ( PARTITION BY featurecode ORDER By accountid DESC) - 1 
  
  FROM....
   GROUP BY edate, featurecode, appcode, accountid
   HAVING appcode='sample' AND eventdate BETWEEN '15-10-2018' And '20-10-2018'
  ) 
  
  Select distinct date, code, total
  from V1
  WHERE date between '2018-10-15' AND '2018-10-20'
  </code></pre>
  
  <p>This returns the same set of values for all the dates. Is there any way to do this efficiently?? It's a DB2 database by the way but I'm looking for insight from postgresql users too. </p>
  
  <pre><code>Present result- All the totals are being repeated.
  
  date        code                 total
  10/15/2018   appname-feature1       123
  10/15/2018   appname-feature2       234
  10/15/2018   appname-feature3       321
  10/16/2018   appname-feature1       123
  10/16/2018   appname-feature2       234
  10/16/2018   appname-feature3       321
  
  Desired result.
  date        code                 total
  10/15/2018   appname-feature1       123
  10/15/2018   appname-feature2       234
  10/15/2018   appname-feature3       321
  10/16/2018   appname-feature1       212
  10/16/2018   appname-feature2       577
  10/16/2018   appname-feature3       2345
  </code></pre>