title: Set value to variable and modify based on condition SQL
body: <p>I have a query where I need to declare a variable and set value to it. Then in the select statement based on one of the column values (<code>max_val</code>) I would need to change the value of the variable if variable value exceeds the value in <code>max_col</code>. The basic query looks like the one below:</p>
  
  <pre><code>Declare @variable int
  Set @variable = x
  
  select * from (
    select 
      *, 
      row_number () over (partition by account_id, product order by revenue desc) as rk 
    from (
      select * 
      from dataset
      where account_id = 'abc'
      and product = 'xyz'
      and @variable between min_val and max_val
    ) x 
  ) y
  where y.rk =1;
  </code></pre>
  
  <p>In this query if <code>@variable &gt; max_val</code>, then no records will be shown. However, I want to include a condition such that when the above criteria matches, <code>@variable</code> is assigned value of <code>max_val</code>.</p>