Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange error using derived table

Status
Not open for further replies.

JohnDTampaBay

Programmer
Jul 12, 2002
986
US
I have the following SELECT that uses a derived, pivot table. I am pivoting a normalized table on the fly.

When run, I am receiving the following error message. What does this mean? How can a table have more columns than * (all columns)?

Server: Msg 8158, Level 16, State 1, Line 2
'aoi' has more columns than were specified in the column list.
----------------------------------------------------------
Code:
Select o.CustID, c.FirstName, c.LastName, c.Suffix,
  c.Title, c.CoName, a.Address1, a.City, a.State,
  a.PostalCode, oi.ProdCode, o.OrderDate, oi.OrderID,
  oi.OrderItemID, aoi.*
From Orders o With (NoLock)
  Join OrderItems oi (NoLock)
    On oi.OrderID = o.OrderId
  Join Customers c (NoLock)
    On c.CustId = o.CustId
  Join (SELECT OrderItemID,  
        MIN(CASE KeyName WHEN 'ZIP' THEN KeyValue
          ELSE NULL END) AS [ZIP],  
        MIN(CASE KeyName WHEN 'COCODE' THEN KeyValue
          ELSE NULL END) AS [COCODE],  
        MIN(CASE KeyName WHEN 'AGE' THEN KeyValue
          ELSE NULL END) AS [AGE],  
        MIN(CASE KeyName WHEN 'STATE' THEN KeyValue
          ELSE NULL END) AS [STATE],  
        MIN(CASE KeyName WHEN 'GENDER' THEN KeyValue
          ELSE NULL END) AS [GENDER],  
        MIN(CASE KeyName WHEN 'GRADE' THEN KeyValue
          ELSE NULL END) AS [GRADE]
        FROM AdditionalOrderInfo (NOLOCK)
        GROUP BY OrderItemID) aoi (NoLock)
    On aoi.OrderItemId = oi.OrderItemId
  Join Address a (NoLock)
    On a.AddressID = oi.ShipToAddress
Where o.OrderDate Between '2/26/2003' And '03/05/2003'
And (oi.status = '01' or oi.status = '03')
And oi.BalanceDue <= 0
Order By o.OrderDate, o.CustID, oi.OrderItemID
----------------------------------------------------------
--Angel [rainbow]
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
In this context

GROUP BY OrderItemID) aoi (NoLock)

(Nolock) is treated as a list of column names for the derived table.
 
Couldn't see the forest for the trees. [hammer]
Thanks, swampBoogie. Works like a charm. --Angel [rainbow]
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top