Solved

Get CASE statement to work with column names that are aggregates

Posted on 2010-08-15
6
319 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
[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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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