Solved

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

Posted on 2007-11-29
6
1,249 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 50

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

688 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