Solved

Get CASE statement to work with column names that are aggregates

Posted on 2010-08-15
6
293 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 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

16 Experts available now in Live!

Get 1:1 Help Now