Solved

Get CASE statement to work with column names that are aggregates

Posted on 2010-08-15
6
298 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

867 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

21 Experts available now in Live!

Get 1:1 Help Now