Solved

Get CASE statement to work with column names that are aggregates

Posted on 2010-08-15
6
312 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
disk usage reporting tools 27 54
SQL eating up memory? 16 41
Make an array show the subkey and put it in a query 2 28
Need to trim my database size 9 24
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

733 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