• subscribe
May 23, 2006 12:00 AM

Catch That Bug!

SQL Server Pro
InstantDoc ID #49985

May's Bug Solution: In SQL Server 2005, you run the following PIVOT query in the Northwind database:

SELECT CustomerID, [1],\[2],\[3], 
  [4],\[5],\[6],\[7],\[8],\[9] 
FROM dbo.Orders 
  PIVOT(COUNT(OrderID) FOR EmployeeID IN([1],\[2],\[3], 
  [4],\[5],\[6],\[7],\[8],\[9])) AS P; 

You expect to get a single row for each customer, along with the number of orders placed by each employee for that customer. Because there are 89 customers in the Orders table that have orders, you expect to get 89 rows in the result. However, you don't get the expected result. Rather, you end up getting 830 rows. Where's the bug in the code?

In the May article, I mentioned that the PIVOT operator has an implicit grouping phase.The implicit grouping list is constructed from all attributes in the table that appears to the left of the PIVOT keyword, excluding attributes that were mentioned in the parentheses following the PIVOT keyword. In our case, the implicit grouping list became CustomerID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, Ship-PostalCode, ShipCountry. You got 830 rows back because there are 830 unique combinations of values in the aforementioned grouping list in the table.

To get the desired result, the PIVOT operator must be provided with an input table that contains only the columns that are relevant to its activity.To achieve this result, you can prepare a derived table or a CommonTable Expression (CTE),as follows:

SELECT CustomerID, [1],\[2],\[3], 
  [4],\[5],\[6],\[7],\[8],\[9] 
FROM (SELECT CustomerID, 
  EmployeeID, OrderID FROM 
  dbo.Orders) AS D 
PIVOT(COUNT(OrderID) 
FOR EmployeeID IN([1],\[2],\[3], 
  [4],\[5],\[6],\[7],\[8],\[9])) AS P; 

The derived table D contains only the relevant columns for PIVOT's activity—that is, OrderID, which is used as the input to the aggregate function; EmployeeID, which contains the elements that you want to rotate; and CustomerID, which will be used as PIVOT's implicit grouping list.

June's Bug: In SQL Server 2005, you create the followingVSortedOrders view in the Northwind database:

USE Northwind; 
GO 
CREATE VIEW dbo.VSortedOrders 
AS 
SELECT TOP(100) PERCENT 
  CustomerID, OrderID, OrderDate 
FROM dbo.Orders 
ORDER BY CustomerID, OrderID; 
GO 

You then issue the following query:

SELECT CustomerID, OrderID, 
  OrderDate 
FROM dbo.VSortedOrders; 

You expect to see the data sorted by customer and order ID, but you don't. Can you identify the bug in the code and suggest a solution?



ARTICLE TOOLS

Comments
  • Claudia
    6 years ago
    Jul 01, 2006

    If you are working in SQL Server 2005 & get the 'compatilility level' error, don't bash your head; check the database properties to see if it is set to SQL Server 2000 or 2005 compatibility level. Works like a charm if you aren't set to another version (hee).

  • Brenda
    6 years ago
    Jun 16, 2006

    Actually, I ran the query after correcting syntax in the View clause and it gave me the correct results. Trick question?

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...