title: How to set value based on value existence in SQL Server?
body: <p>I have the following T-SQL code:</p>
  
  <pre><code>select 
      id,
      (case
          when n in(Bla1', 'Bla2') then 1
          when n = 'Bla3' then 99
          else 0
       end) as c
  from 
      hello
  </code></pre>
  
  <p>Running this code outputs this result:</p>
  
  <pre><code>|  id    | c  |
  +--------+----+
  | 577140 | 0  |
  | 577140 | 1  |
  | 577140 | 0  |
  | 577140 | 0  |
  | 577140 | 99 |
  | 577141 | 0  |
  | 577141 | 0  |
  | 577141 | 0  |
  | 577142 | 0  |
  | 577142 | 0  |
  | 577142 | 1  |
  </code></pre>
  
  <p>How can I modify the code to get the following output?</p>
  
  <pre><code>|  id    | c  |
  +--------+----+
  | 577140 | 99 |
  | 577141 | 0  |
  | 577142 | 1  |
  </code></pre>
  
  <p><strong>Rule</strong></p>
  
  <p>For each <code>id</code>: If <code>99</code> exists, then <code>c</code> becomes <code>99</code>. If not, either <code>1</code> or <code>0</code>, depending if any <code>1</code> exists.</p>