Solved

Microsoft SQL Sub  Query - How do I use a "where" clause on a "Sum" column

Posted on 2007-11-29
6
1,246 Views
Last Modified: 2012-06-27
I am using Microsoft SQL Server 2000.

I have the following query below am using the query below:

Select C.ComponentID,

(SELECT     SUM(IsNull(OrderQuantity, 0)) - SUM(IsNull(ReceiveQuantity, 0))
  FROM          PurchaseOrderItemTable I
  WHERE      C.ComponentID = I.ComponentID AND I.ItemStatusID IN (0, 2)) AS ComponentsOnOrder,
(SELECT     SUM(IsNull(Quantity * PT.ProductionQuantity, 0))
  FROM          ProductionItemTable P LEFT OUTER JOIN
  ProductionTable PT ON PT.ProductionID = P.LinkID
  WHERE      C.ComponentID = P.ItemLinkID AND P.ItemTypeID = 1 AND PT.StatusID = 1) AS ComponentsAllocated

From ComponentTable C
  where C.StockHolding = 1
and
(C.StockOnHand - ComponentsAllocated) + ComponentsOnOrder) < C.ReorderLevel

The problem is that I am unable to use the "ComponentsAllocated" or "ComponentsOnOrder" in my final "Where" clause as I get the error message :

"Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'ComponentsAllocated'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'ComponentsOnOrder'."

I know the query works as if I remove the last two line all is OK.

The query is used to retun a list of "ComponentID"s from the ComponentTable and the two sub-queries are calculating the Components OnOrder and the Components Allocated from the respective tables for Each Component.

The last two lines of the query only return components that need to be re-ordered.

Any help would be much appreciated.
0
Comment
Question by:saphireneil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 20375927
That columns have the same name of your alias to the 2 subqueries.
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 20375954
You are using table alias names rather than column names. Try:
Select C.ComponentID,

(SELECT     SUM(IsNull(OrderQuantity, 0)) - SUM(IsNull(ReceiveQuantity, 0)) As ComponentsOnOrder
  FROM          PurchaseOrderItemTable I
  WHERE      C.ComponentID = I.ComponentID AND I.ItemStatusID IN (0, 2)) AS Table1,
(SELECT     SUM(IsNull(Quantity * PT.ProductionQuantity, 0)) AS ComponentsAllocated
  FROM          ProductionItemTable P LEFT OUTER JOIN
  ProductionTable PT ON PT.ProductionID = P.LinkID
  WHERE      C.ComponentID = P.ItemLinkID AND P.ItemTypeID = 1 AND PT.StatusID = 1) AS Table2

From ComponentTable C
  where C.StockHolding = 1
and
(C.StockOnHand - ComponentsAllocated) + ComponentsOnOrder) < C.ReorderLevel

0
 
LVL 5

Expert Comment

by:usarian
ID: 20376029
You cant reference subqueries in a WHERE clause.

subqueries are processed row by row returned, the where clause designates the criteria to return the row.  You will either have to puyt the whoel subqueries in the where clause or find a way to move the subqueries to JOINs.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:usarian
ID: 20376119
Try this:

Select C.ComponentID, ComponentsAllocated.Allocated, ComponentsOnOrder.OnOrder
 
From ComponentTable C
 
LEFT OUTER JOIN
(SELECT P.ItemLinkID, SUM(IsNull(Quantity * PT.ProductionQuantity, 0)) AS Allocated
  FROM ProductionItemTable P 
 
LEFT OUTER JOIN
  ProductionTable PT ON PT.ProductionID = P.LinkID
WHERE P.ItemTypeID = 1 AND PT.StatusID = 1)
   AS ComponentsAllocated ON C.ComponentID = ComponentsAllocated.ItemLinkID
 
LEFT OUTER JOIN
(SELECT I.ComponentID, SUM(IsNull(OrderQuantity, 0)) - SUM(IsNull(ReceiveQuantity, 0)) AS OnOrder
  FROM PurchaseOrderItemTable I WHERE I.ItemStatusID IN (0, 2))
   AS ComponentsOnOrder ON C.ComponentID = ComponentsOnOrder.ComponentID
 
  where C.StockHolding = 1 
and
(C.StockOnHand - ComponentsAllocated.Allocated) + ComponentsOnOrder.OnOrder) < C.ReorderLevel

Open in new window

0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20376388
What you usually do is wrap the whole thing as another subquery like so
SELECT * FROM
(
Select C.ComponentID,
 
(SELECT     SUM(IsNull(OrderQuantity, 0)) - SUM(IsNull(ReceiveQuantity, 0))
  FROM          PurchaseOrderItemTable I
  WHERE      C.ComponentID = I.ComponentID AND I.ItemStatusID IN (0, 2)) AS ComponentsOnOrder,
(SELECT     SUM(IsNull(Quantity * PT.ProductionQuantity, 0))
  FROM          ProductionItemTable P LEFT OUTER JOIN
  ProductionTable PT ON PT.ProductionID = P.LinkID
  WHERE      C.ComponentID = P.ItemLinkID AND P.ItemTypeID = 1 AND PT.StatusID = 1) AS ComponentsAllocated
 
From ComponentTable C
  where C.StockHolding = 1
) DATA
WHERE (C.StockOnHand - ComponentsAllocated) + ComponentsOnOrder < C.ReorderLevel

Open in new window

0
 

Author Closing Comment

by:saphireneil
ID: 31411817
Apart from a couple of minor syntax changes this is spot on! thanks alot...

I have detailed below the final query below for future ee reference.

Thanks again

Neil :-)

-----------------------------------------------------------------

SELECT * FROM
(
Select C.ComponentID, C.StockOnHand, C.ReorderLevel,
 
(SELECT     SUM(IsNull(OrderQuantity, 0)) - SUM(IsNull(ReceiveQuantity, 0))
  FROM          PurchaseOrderItemTable I
  WHERE      C.ComponentID = I.ComponentID AND I.ItemStatusID IN (0, 2)) AS ComponentsOnOrder,
(SELECT     SUM(IsNull(Quantity * PT.ProductionQuantity, 0))
  FROM          ProductionItemTable P LEFT OUTER JOIN
  ProductionTable PT ON PT.ProductionID = P.LinkID
  WHERE      C.ComponentID = P.ItemLinkID AND P.ItemTypeID = 1 AND PT.StatusID = 1) AS ComponentsAllocated
 
From ComponentTable C
  where C.StockHolding = 1
)
 DATA
WHERE (StockOnHand - ComponentsAllocated) + ComponentsOnOrder < ReorderLevel
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question