title: SQL sequential IF conditions
body: <p>I have a table called <code>Tasks</code>. I want to sum the number of tasks status types (open, closed) and priorities grouped by project.</p>
  
  <p>Something like this:</p>
  
  <pre><code>===============================================================================================
  | Project   |   Closed  |   Major   |   Moderate    |   Minor   | Total 
  ===============================================================================================
  |Project 1  |   4       |   3       |   0           |   0       | 7
  |Project 2  |   1       |   0       |   2           |   5       | 8
  |Project 3  |   10      |   8       |   3           |   4       | 25
  </code></pre>
  
  <p>The "priority" is determined by the ability to find a keyword within a VarChar field. (I know, very bad, but no problems there)</p>
  
  <p><em>The part I can't figure out</em> is how can I write the query so that IF I determine priority is "Major", break, or move on to the next task. Once I've determined the priority is "Major" the other priorities (Moderate, and Minor) should be ignored. The same goes for "Moderate". If I find "Moderate" I should not worry about Minor. <strong>This is the part I'm having trouble with.</strong> <strong>How do I create an if statement based on results from another select?</strong></p>
  
  <p>This is what I have so far which gives me all the counts...but the priorities are not "chained" or dependent on the match condition of the parent.</p>
  
  <pre><code>SELECT  Project, 
          count(status) as "Status Count"
          ,sum(case when status = 'Closed' then 1 else 0 end) as Closed
          ,sum(case when status = 'Open' then 1 else 0 end) as "Open"
          ,sum(case when CHARINDEX ( 'Major', DefectSummary COLLATE Latin1_General_CI_AS) &gt; 0 then 1 else 0 end) as Major
          ,sum(case when CHARINDEX ( 'Moderate', DefectSummary COLLATE Latin1_General_CI_AS) &gt; 0 then 1 else 0 end) as Moderate
          ,sum(case when CHARINDEX ( 'Minor', DefectSummary COLLATE Latin1_General_CI_AS) &gt; 0 then 1 else 0 end) as Minor
  
  FROM Tasks
  GROUP BY Project
  </code></pre>
  
  <p><strong><em>DISCLAIMER</strong>: I know this is a terrible data model. I inherited it from a previous developer. I'm in the process of overhauling the entire system but I need to be able to generate reports as it is now until I finish the new system.</em></p>