Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This type of data transformation is known as a <em>pivot</em>. There are several ways that you can convert the row data into columns.</p> <p>You can use an aggregate function with a <code>CASE</code>:</p> <pre><code>select site, sum(case when Status = 'Not Started' then 1 else 0 end) [Not Started], sum(case when Status = 'In Progress' then 1 else 0 end) [In Progress], sum(case when Status = 'Testing' then 1 else 0 end) Testing, sum(case when Status = 'Complete' then 1 else 0 end) Complete from sitedata group by site </code></pre> <p>See <a href="http://sqlfiddle.com/#!3/b30c4/1" rel="nofollow">SQL Fiddle with Demo</a>.</p> <p>You will replace the <code>sitedata</code> with your existing query.</p> <p>Or you can use the <a href="http://msdn.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx" rel="nofollow"><code>PIVOT</code></a> function:</p> <pre><code>select site, [Not Started], [In Progress], Testing, Complete from ( select [Site], Status from sitedata ) src pivot ( count(Status) for Status in ([Not Started], [In Progress], Testing, Complete) ) piv </code></pre> <p>See <a href="http://sqlfiddle.com/#!3/b30c4/4" rel="nofollow">SQL Fiddle with Demo</a>.</p> <p>If you have an unknown number of <code>Status</code> values, then you can use dynamic SQL:</p> <pre><code>DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select @cols = STUFF((SELECT distinct ',' + QUOTENAME([Status]) from SiteData FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT site, ' + @cols + ' from ( select [Site], Status from sitedata ) x pivot ( count(Status) for Status in (' + @cols + ') ) p ' execute(@query) </code></pre> <p>See <a href="http://sqlfiddle.com/#!3/b30c4/6" rel="nofollow">SQL Fiddle with Demo</a>.</p> <p>The result for each query is the same:</p> <pre><code>| SITE | NOT STARTED | IN PROGRESS | TESTING | COMPLETE | --------------------------------------------------------- | 1 | 2 | 1 | 1 | 1 | | 2 | 1 | 2 | 2 | 0 | | 3 | 2 | 0 | 1 | 1 | </code></pre> <p>Edit #1, if you want a <code>Total</code> row, then you can use:</p> <pre><code>select case when site is not null then cast(site as varchar(10)) else 'Total' end Site, sum([Not Started]) [Not Started], Sum([In Progress])[In Progress], Sum(Testing) Testing, sum(Complete) Complete, Sum([Not Started]+[In Progress]+ Testing+ Complete) Total from ( select [Site], Status from sitedata ) src pivot ( count(Status) for Status in ([Not Started], [In Progress], Testing, Complete) ) piv group by rollup (site) </code></pre> <p>See <a href="http://sqlfiddle.com/#!3/b30c4/16" rel="nofollow">SQL Fiddle with Demo</a></p>
    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