title: Teradata SQL flag for last X distinct column permutations by rank
body: <p>My Teradata SQL query finds all unique fiscal year/fiscal week combinations in a table and orders them chronologically:</p>
  
  <pre><code>select
  fiscalyear,fiscalweek,
  fiscalyear||fiscalweek as yrwkperm
  from table
  qualify count(*) over (partition by yrwkperm rows unbounded preceding) = 1
  order by fiscalyear,fiscalweek
  </code></pre>
  
  <p>I then take the resulting dataset and add another column that designates the chronological order of the weeks, but is simply the row number since I've ordered by fiscalyear,fiscalweek:</p>
  
  <p><a href="http://i.stack.imgur.com/N1d8E.png" rel="nofollow">Excel example screenshot</a></p>
  
  <p>I then add another logical column that says (pseudocode)
  If (chrono >= (max(chrono)-52+1)) then "Last52Weeks" else "NotLast52Weeks"</p>
  
  <p>The expression in the parentheses will be TRUE if the fiscalyear/fiscalweek permutation is in the last 52 weeks of the dataset.  (I have no discontinuities in the list of distinct fiscalyear/fiscalweek permutations so I have no concerns about the row numbers not being the same as the chronological order of the weeks).</p>
  
  <p>So my question is, can I create a column with the "last 52 weeks yes/no" binary flag, all inside a single TD SQL query?</p>
  
  <p>Thanks in advance for any help.</p>