Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>There are similar questions <a href="https://stackoverflow.com/questions/1946234/convert-database-rows-into-columns">here</a>,<a href="https://stackoverflow.com/questions/801895/sql-server-convert-rows-to-columns">here</a> answered in stackoverflow.</strong> </p> <p>You need to use the operator <strong><a href="http://msdn.microsoft.com/en-us/library/ms177410.aspx" rel="nofollow noreferrer">PIVOT</a></strong> in your query to acheive this.Here is the example and explanation on how you can do that.The example is referenced from <strong><a href="http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx" rel="nofollow noreferrer">this</a></strong> source. </p> <pre><code>---I assumed your tablename as TESTTABLE--- DECLARE @cols NVARCHAR(2000) DECLARE @query NVARCHAR(4000) SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + t.Name FROM TESTTABLE AS t ORDER BY '],[' + t.Name FOR XML PATH('') ), 1, 2, '') + ']' SET @query = N'SELECT '+ @cols +' FROM (SELECT t1.Name , t1.Count FROM TESTTABLE AS t1) p PIVOT (MAX([Count]) FOR Name IN ( '+ @cols +' )) AS pvt;' EXECUTE(@query) </code></pre> <p><strong>Explanation</strong> </p> <p>1.The first part of the query </p> <pre><code>SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + t.Name FROM TESTTABLE AS t ORDER BY '],[' + t.Name FOR XML PATH('') ), 1, 2, '') + ']' </code></pre> <p>gives you a nice flattened result of your Name column values in a single row as follow</p> <pre><code>[Cheryl],[Drew],[Karen],[Kath],[Kirk],[Matt] </code></pre> <p><strong>You can learn more about the STUFF and XML PATH <a href="http://msdn.microsoft.com/en-us/library/ms188043.aspx" rel="nofollow noreferrer">here</a> and <a href="http://msdn.microsoft.com/en-us/library/ms345137SQL.90.aspx" rel="nofollow noreferrer">here</a>.</strong> </p> <p>2.<code>SELECT + @cols + FROM</code> will select all the rows as coloumn names for the final result set (pvt - step 3) </p> <p>i.e </p> <pre><code>Select [Chery],[Drew],[Morgan],[Kath],[Kirk],[Matt] </code></pre> <p>3.This query pulls all the rows of data that we need to create the cross-tab results. The (p) after the query is creating a temporary table of the results that can then be used to satisfy the query for step 1.</p> <pre><code>(SELECT t1.Name, t1.Count FROM TESTTABLE AS t1) p </code></pre> <p>4.The PIVOT expression </p> <pre><code>PIVOT (MAX (Count) FOR Name IN ( @cols) AS pvt </code></pre> <p>does the actual summarization and puts the results into a temporary table called pvt as </p> <pre><code>Chery | Drew | Morgon | Kath | Kirk | Matt ------------------------------------------- 257 1500 13 500 200 76 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload