Stephen Forte's WebBlog - The Rozenshtein Method
I have to admit that I am totally hooked on the The Rozenshtein Method. My buddy, Richard Campbell showed it to me a year or two ago and I have been hooked ever since. I recently demoed it at TechED in
Here is how it works. You need a crosstab query. You have to move rows into columns. You also need
First let’s look at the desired results. We want to take the orders data from Northwind and pivot the sales date (aggregated by month) as columns with the sum of the total sales in the row grouped by customer. It would look something like this:
CompanyName TotalAmount Jan Feb Mar…(etc)
Company1 100 25 33 10
Company2 467 76 62 87
(etc)
The TSQL query to do this is, go ahead and run it in Northwind in SQL Server:
SELECT CompanyName, SUM((UnitPrice*Quantity)) As TotalAmt,
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
SUM((UnitPrice*Quantity)*(1-
FROM Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN
[Order Details] ON Orders.OrderID = [Order Details].OrderID
Group By Customers.CompanyName
So how does this work?
This method uses Boolean aggregates, so that each column has a numeric expression that resolves each row as a zero or one and that value (0 or 1) is multiplied by your numeric expression (Like TotalSales or (UnitPrice*Quantity). That is all there is to it, quite simple. But wait, there’s more to explain:
We want to create columns for each Month in our data. To find a month use DatePart. But we need to subtract the DatePart value (1-12) from the amount you’re looking for (1 for Jan, 2 for Feb, etc) as shown here for January:
DatePart(mm,OrderDate)-1
So that true = zero, false > 0 or <>
Next you have to compute the sign of the expression and get the absolute value like so:
This will give us a positive value. Remember 0 is still true. Now subtract the value computed from 1 in order to get a 0 or 1 from the value of your expression (the Boolean aggregate). The code is:
(1-
For example if you had March return 3 from the Datepart, 3-1=2 and 1-2 =-1. The absolute value is 1. This will always return 0 or 1. If your expression was zero, the value is now one. If was one, the value is zero.
Last step. Taking the SUM of the Boolean values will give you a count of the values that qualify. So you can find out how many sales you made in Jan, Feb, etc. So now multiply the value by the price and quantity, but remember its now one = true. Take a look here:
SUM((UnitPrice*Quantity)*(1-
If its zero, nothing gets added, if its one, you get the value of the sale. The sum of the total expression is the total of sales for the month. If you have a DatePart that is evaluated to 0 then ((UnitPrice*Quantity)*0) is 0 and those results are ignored in the SUM. If you have a month that matches your expression resolves to 1 and ((UnitPrice*Quantity)*1) is the value of the sale.
How easy!
But wait, there’s more! Suppose you wanted two values combined? Compute each value down to zero or one separately. Now you can use
Ok, have fun!!!
No comments:
Post a Comment