Solved

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

Posted on 2007-11-29
6
1,242 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
6 Comments
 
LVL 45

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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now