title: Aggregating different rows in one column SQL Query
body: <p>Sorry, I didn't come up with a good title for the question, so feel free to change it accordingly.</p>
  
  <p>I may describe my question with a minimal example in MS SQL server 2012:</p>
  
  <pre><code> create table #tmp
  (
    RowID varchar(10),
    SectionCode int,
    SectionName varchar(10)
  )
  
  insert into #tmp values('Record1' , 1 , 'AB');
  insert into #tmp values('Record1' , 2 , 'CD');
  insert into #tmp values('Record1' , 3 , 'EF');
  insert into #tmp values('Record2' , 1 , 'AB');
  insert into #tmp values('Record2' , 4 , 'GH');
  insert into #tmp values('Record2' , 5 , 'IJ');
  insert into #tmp values('Record3' , 2 , 'CD');
  insert into #tmp values('Record3' , 5 , 'IJ');
  </code></pre>
  
  <p>I am trying to create a one row per record result in which every section is a column and if there is a row associated with a section, the corresponding column value is increased. This is (not) what I want (the same record data on different rows)</p>
  
  <pre><code>select  RowID, 
      case when SectionName = 'AB' then 1 else 0 end as [AB Section] , 
      case when SectionName = 'CD' then 1 else 0 end as [CD Section] , 
      case when SectionName = 'EF' then 1 else 0 end as [EF Section] , 
      case when SectionName = 'GH' then 1 else 0 end as [GH Section] , 
      case when SectionName = 'IJ' then 1 else 0 end as [IJ Section] 
                                  from #tmp
  group by RowID , SectionName
  </code></pre>
  
  <p>which gives this output:</p>
  
  <p><a href="https://i.stack.imgur.com/RG97H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RG97H.png" alt="Wrong Output Result"></a></p>
  
  <p>I need this:</p>
  
  <p><a href="https://i.stack.imgur.com/4ae2T.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4ae2T.jpg" alt="Correct Output"></a></p>
  
  <p>Thanks in advance</p>