|
|
Sunday, August 10, 2003 |
|
Source: Nick's Blog says: Marc just sent me a query showing how to query for a "%" symbol in a LIKE statement.
SELECT * Updated 1:42pm: Here is a list of characters that you can use in a LIKE statement and their function:
5:11:19 PM |
|
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 < 0. For example if the month you were looking for was January and the DatePart was 1 and you subtract 1 from that value you get 0, which is true. If you are looking for March you would get -2 and that would be false. 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!!! [Stephen Forte's WebBlog]8:05:41 AM |
