Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In SQL Server 2008 you can handle this task pretty easily with a PIVOT query. The following example relies on getting your data into the following format (which it looks like you have done already):</p> <pre><code>Name Month Value ---------- ------- ----- District 1 Month 1 10 District 1 Month 2 5 District 1 Month 3 6 District 2 Month 1 1 District 2 Month 2 2 District 2 Month 3 3 District 3 Month 1 8 District 3 Month 2 6 District 3 Month 3 11 </code></pre> <p>If you can do that, then your PIVOT query should look something like this:</p> <pre><code>DECLARE @myTable AS TABLE([Name] VARCHAR(20), [Month] VARCHAR(20), [Value] INT) INSERT INTO @myTable VALUES ('District 1', 'Month 1', 10) INSERT INTO @myTable VALUES ('District 1', 'Month 2', 5) INSERT INTO @myTable VALUES ('District 1', 'Month 3', 6) INSERT INTO @myTable VALUES ('District 2', 'Month 1', 1) INSERT INTO @myTable VALUES ('District 2', 'Month 2', 2) INSERT INTO @myTable VALUES ('District 2', 'Month 3', 3) INSERT INTO @myTable VALUES ('District 3', 'Month 1', 8) INSERT INTO @myTable VALUES ('District 3', 'Month 2', 6) INSERT INTO @myTable VALUES ('District 3', 'Month 3', 11) SELECT [Name], [Month 1], [Month 2], [Month 3], [NameTotalValue] AS [Total] FROM ( SELECT [Name], [Month], [Value], SUM([Value]) OVER (PARTITION BY [Name]) as [NameTotalValue] FROM @myTable UNION SELECT 'Total', [Month], SUM([Value]), (SELECT SUM([Value]) FROM @myTable) FROM @myTable GROUP BY [Month] ) t PIVOT ( SUM([Value]) FOR [Month] IN ([Month 1], [Month 2], [Month 3]) ) AS pvt ORDER BY pvt.[Name] </code></pre> <p>In this example, I used the <code>SUM([Value]) OVER PARTITION</code> to get the sums for each District, and then I did a UNION to add a totals row to the bottom. The results look like this:</p> <pre><code>Name Month 1 Month 2 Month 3 Total ----------- ------- ------- ------- ----- District 1 10 5 6 21 District 2 1 2 3 6 District 3 8 6 11 25 Total 19 13 20 52 </code></pre> <p>One thing you'll notice about this approach is that you have to know the column names you want at the top of the table ahead of time. That's easy to do if you're setting up the report to run for a full year, but is trickier if the number of columns is going to change. If you're going to allow the users to specify a custom date range (i.e., 07/2011-10/2011 or 06/2011-11/2011), then one way handle that requirement is to build the PIVOT query using dynamic SQL and then execute it with <a href="http://msdn.microsoft.com/en-us/library/ms188001.aspx" rel="nofollow">sp_executesql</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