title: How to create student mark sheet like Turm in SQL Server
body: <p>I want to show a student mark sheet like this:</p>
  
  <pre><code>---------------------------------------------------------
                      FIRST TURM   | SECOND TURM|
  ---------------------------------------------------------
   S_ID | SUB_NAME | OutOF | Marks | OutOF | Marks 
  ---------------------------------------------------------
    101    ENGLISH      100     90     100 90
    101    HISTORY      100     80     100 80
    101    MATHS        140     80     140 80
    101    SANSKRIT     100     75     100 75
    102    ENGLISH      100     80     100 80 
    102    HISTORY      100     80     100 80 
    102    MATHS        140     80     140 80  
    102    SANSKRIT     100     80     100 80
  </code></pre>
  
  <p>I use pivot keyword for this i get this output </p>
  
  <pre><code>create table #t 
  (
      S_ID int, SUB_NAME varchar(10), OutOF int, Marks int, Turm int
  )
  
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(101,&amp;#39;MATHS&amp;#39;,1,140,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(101,&amp;#39;ENGLISH&amp;#39;,1,100,90)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(101,&amp;#39;SANSKRIT&amp;#39;,1,100,75)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(101,&amp;#39;HISTORY&amp;#39;,1,100,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(102,&amp;#39;MATHS&amp;#39;,1,140,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(102,&amp;#39;ENGLISH&amp;#39;,1,140,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(102,&amp;#39;SANSKRIT&amp;#39;,1,140,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(102,&amp;#39;HISTORY&amp;#39;,1,140,80)
  
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(101,&amp;#39;MATHS&amp;#39;,2,140,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(101,&amp;#39;ENGLISH&amp;#39;,2,100,90)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(101,&amp;#39;SANSKRIT&amp;#39;,2,100,75)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(101,&amp;#39;HISTORY&amp;#39;,2,100,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(102,&amp;#39;MATHS&amp;#39;,2,140,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(102,&amp;#39;ENGLISH&amp;#39;,2,140,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(102,&amp;#39;SANSKRIT&amp;#39;,2,140,80)
  insert into #t (S_ID,SUB_NAME,turm,OutOF ,Marks)VALUES(102,&amp;#39;HISTORY&amp;#39;,2,140,80)
  
  select * 
  from 
      (SELECT 
          S_ID, SUB_NAME,
          CASE WHEN turm = 1 THEN &amp;#39;FIRST TURM&amp;#39; ELSE &amp;#39;SECOND TURM&amp;#39; END AS turm,
          OutOF, Marks 
       FROM #t) S
  PIVOT 
  ( 
     MAX(Marks) FOR [turm] IN ([FIRST TURM], [SECOND TURM])) S
  
  drop  table  #t
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>S_ID | SUB_NAME | OutOF |FIRST  TURM |SECOND TURM 
  --------------------------------------------------
  101 ENGLISH  100      90              90
  101 HISTORY  100      80              80
  101 MATHS    140      80              80
  101 SANSKRIT 100      75              75
  102 ENGLISH  140      80              80
  102 HISTORY  140      80              80
  102 MATHS    140      80              80
  102 SANSKRIT 140      80              80
  </code></pre>