Sorry for the slow reply. Lod is correct in that the code I suggested is meant to accommodate NULLS in the PaperSize column.
Let's image we have a table like this:
[tt]
PaperSize TotalCount
--------- ----------
Letter 10
Letter 20
Small 100
Small 200
Big 300
NULL 1000
[/tt]
total letter count should be 30, right? What about the non-letter count? You probably think it should be 1600 (because the NULL PaperSize should be included).
To explain this, let's write some code. To follow the examples, open a query window in SQL Server Management Studio and copy/paste the code blocks to see how the code works.
The following code sets up a table variable and hard codes some data. This allows us to play with the query without actually affecting any data you have in any of your tables.
Code:
Declare @Temp Table(PaperSize VarChar(20), TotalCount int)
Insert Into @Temp Values('Letter',10)
Insert Into @Temp Values('Letter',20)
Insert Into @Temp Values('Small', 100)
Insert Into @Temp Values('Small', 200)
Insert Into @Temp Values('Big', 300)
Insert Into @Temp Values(NULL, 1000)
Now, let me show you how the case statements affect the data. When thinking about case statements, you should think of them as operating on a row-by-row basis.
Code:
Declare @Temp Table(PaperSize VarChar(20), TotalCount int)
Insert Into @Temp Values('Letter',10)
Insert Into @Temp Values('Letter',20)
Insert Into @Temp Values('Small', 100)
Insert Into @Temp Values('Small', 200)
Insert Into @Temp Values('Big', 300)
Insert Into @Temp Values(NULL, 1000)
Select PaperSize,
TotalCount,
Case When PaperSize = 'Letter' Then TotalCount Else 0 End As LetterColumn,
Case When PaperSize = 'Letter' Then 0 Else TotalCount End As NonLetterColumn,
Case When PaperSize <> 'Letter' Then TotalCount Else 0 End As NotLetter
From @Temp
When you run the code above, you will get:
[tt][blue]
PaperSize TotalCount LetterColumn NonLetterColumn NotLetter
-------------------- ----------- ------------ --------------- -----------
Letter 10 10 0 0
Letter 20 20 0 0
Small 100 0 100 100
Small 200 0 200 200
Big 300 0 300 300
NULL 1000 0 1000 [!]0[/!]
[/blue][/tt]
Notice how we get 0's for some of the case statements and actual values for other case statements. Also notice the single piece of data highlighted in red above. This is the case statement that uses PaperSize <> 'Letter'. You see, the PaperSize is NULL for that row and NULL cannot be compared with a value, so it is not included in the case and therefore returns 0.
Next, I hope you can see that summing the values from the output of the case statements will return the data you are looking for. So...
Code:
Declare @Temp Table(PaperSize VarChar(20), TotalCount int)
Insert Into @Temp Values('Letter',10)
Insert Into @Temp Values('Letter',20)
Insert Into @Temp Values('Small', 100)
Insert Into @Temp Values('Small', 200)
Insert Into @Temp Values('Big', 300)
Insert Into @Temp Values(NULL, 1000)
Select Sum(Case When PaperSize = 'Letter' Then TotalCount End) As LetterCount,
Sum(Case When PaperSize <> 'Letter' Then TotalCount End) As NonLetterCount,
Sum(Case When PaperSize = 'Letter' Then 0 Else TotalCount End) As AllNonLetterCount
From @Temp
The query shown above returns:
[tt][blue]
LetterCount NonLetterCount AllNonLetterCount
----------- -------------- -----------------
30 600 1600
[/blue][/tt]
Notice the column labelled NonLetterCount and the other one AllNonLetterCount. Depending on your data, it may not be possible for those columns to return different values. The only way to get different values from these columns is if there are NULLs in the PaperSize column. If your table does not allow nulls, or there simply aren't any nulls, then the NonLetterCount and the AllNonLetterCount columns will return the same value.
Does this help you to understand the query?
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom