Solved

Get CASE statement to work with column names that are aggregates

Posted on 2010-08-15
6
303 Views
Last Modified: 2012-05-10
Below I have attached the Query and the Result Set and my attempt at the CASE function.

In the result set you'll see it returns
  the Qty of a product available in stock as QtyInStock
  AND the Qty that was ordered as 'QtyOrdered'.
I want to have another column called 'QtyToDeliver' which works like this:
If
   QtyInStock < QtyOrdered
THEN
   QtyToDeliver = QtyInStock
ELSE
  QtyToDeliver = QtyOrdered

With my CASE function like it is I get error:
Incorrect syntax near '<'.
or
Invalid column name 'QtyInStock'.

====================================
==THE RESULTSET
====================================
PRODUCT_ID  QtyInStock QtyOrdered
1032        1          1
1016        10         1
1018        0          1

==========================
===THE QUERY
==========================
SELECT
  Product.PRODUCT_ID,
  IsNull(Count(Stock.STOCK_ID),0) As QtyInStock,
  (SELECT 
     TRANS_CLIENT_STOCK.QTY 
   FROM 
     TRANS_CLIENT_STOCK
   WHERE 
     TRANS_CLIENT_STOCK.TC_ID = 94403 
     AND TRANS_CLIENT_STOCK.PRODUCT_ID = Product.PRODUCT_ID) AS QtyOrdered
--,
--QtyToDeliver = CASE QtyInStock
--WHEN QtyInStock < QtyOrdered THEN QtyOrdered
--ELSE QtyOrdered
--END
FROM
  Product
    LEFT JOIN Stock
      ON Product.PRODUCT_ID = Stock.PRODUCT_ID
      AND Stock.CLIENT_ID = 691
      AND POS_ID IS NULL
      AND PickedDate IS NULL 
WHERE
  PRODUCT.PRODUCT_ID NOT IN
   (Select PRODUCT_ID FROM EXT_TYPE) 
Group by
  Product.PRODUCT_ID,
  Product.SortOrder 
order by
  Product.SortOrder

Open in new window

0
Comment
Question by:rfwoolf
  • 3
  • 2
6 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 33442981
, QtyToDeliver = CASE WHEN QtyInStock < QtyOrdered THEN QtyOrdered ELSE QtyOrdered END
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33442990
dale_burrell>

Msg 207, Level 16, State 1, Line 14
Invalid column name 'QtyInStock'.
Msg 207, Level 16, State 1, Line 14
Invalid column name 'QtyOrdered'.
Msg 207, Level 16, State 1, Line 14
Invalid column name 'QtyOrdered'.
Msg 207, Level 16, State 1, Line 15
Invalid column name 'QtyOrdered'.
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33443001
..as you can see, QtyInStock is an aggregate column, maybe that's the problem? :
  IsNull(Count(Stock.STOCK_ID),0) As QtyInStock,
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33443011
SELECT PRODUCT_ID, QtyInStock, QtyOrdered,
QtyToDeliver = CASE
WHEN QtyInStock < QtyOrdered THEN QtyOrdered
ELSE QtyOrdered
END
FROM
(
SELECT
  Product.PRODUCT_ID,
  Product.SortOrder,
  IsNull(Count(Stock.STOCK_ID),0) As QtyInStock,
  (SELECT
     TRANS_CLIENT_STOCK.QTY
   FROM
     TRANS_CLIENT_STOCK
   WHERE
     TRANS_CLIENT_STOCK.TC_ID = 94403
     AND TRANS_CLIENT_STOCK.PRODUCT_ID = Product.PRODUCT_ID) AS QtyOrdered
FROM
  Product
    LEFT JOIN Stock
      ON Product.PRODUCT_ID = Stock.PRODUCT_ID
      AND Stock.CLIENT_ID = 691
      AND POS_ID IS NULL
      AND PickedDate IS NULL
WHERE
  PRODUCT.PRODUCT_ID NOT IN
   (Select PRODUCT_ID FROM EXT_TYPE)
Group by
  Product.PRODUCT_ID,
  Product.SortOrder
) SQ
order by
  SortOrder
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33443028
Thanks cyberkiwi... I don't know if anyone's ever pointed out that you seem to be pretty good at this stuff :)
Can you confirm though that you agree this is because QtyInStock was part of an aggregate function or something.. now that you've taken the CASE statement out of the main result set, it works no problem ??
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 500 total points
ID: 33443096
It is not so much aggregate or not, it is the fact that it is a derived column.
Aggregation makes a column, so do functions like dateadd and expressions like field+3 etc.

You have two choices - repeat the entire expression (not friendly given one of them is a subquery) or put the whole query into a subquery.

Regards
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

810 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