473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

CASE statement in subquery

Uncle Dickie
Hi All,

I have the following bit of code which looks at a parts list and then checks on incoming and outgoing movements in the next 60 days

Expand|Select|Wrap|Line Numbers
  1. SELECT        pa.PartNumber
  2.             ,pa.OnHandStockLevel
  3.             ,(SELECT  sum(pod.QuantityPurchased - pod.QuantityReceived)
  4.               FROM    Purchase.PurchaseOrderDetails pod
  5.               WHERE   pod.ReceiptStatusID NOT IN (3,4)
  6.               AND     pod.PartID = pa.PartID
  7.               AND     pod.ReceiptDate < (getdate()+60)) - 
  8.              (SELECT  sum(sod.QuantityOrdered - sod.QuantityDespatched)
  9.               FROM    Sales.SalesOrderDetails sod
  10.               WHERE   sod.DespatchStatusID NOT IN (3,2)
  11.               AND     sod.PartID = pa.PartID
  12.               AND     sod.OnHold = 0
  13.               AND     sod.DespatchDate < (getdate()+60)) 
  14. FROM        Structure.Parts pa
  15.  
This does pretty much what I want it to do except that there may well be no purchases or sales in the time frame.

In this case in need the result of each of the sub queries to be 0 rather than 'NULL' so that the calculation of purchase - sales will still work.

I have tried using a CASE statement but it returns an error each time.

Any suggestions?
Feb 4 '09 #1
1 2311
Found a solution ISNULL works a treat so just ignore me!

Expand|Select|Wrap|Line Numbers
  1. SELECT        pa.PartNumber
  2.             ,pa.OnHandStockLevel
  3.             ,ISNULL((SELECT  sum(pod.QuantityPurchased - pod.QuantityReceived)
  4.               FROM    Purchase.PurchaseOrderDetails pod
  5.               WHERE   pod.ReceiptStatusID NOT IN (3,4)
  6.               AND     pod.PartID = pa.PartID
  7.               AND     pod.ReceiptDate < (getdate()+60)),0) - 
  8.              ISNULL((SELECT  sum(sod.QuantityOrdered - sod.QuantityDespatched)
  9.               FROM    Sales.SalesOrderDetails sod
  10.               WHERE   sod.DespatchStatusID NOT IN (3,2)
  11.               AND     sod.PartID = pa.PartID
  12.               AND     sod.OnHold = 0
  13.               AND     sod.DespatchDate < (getdate()+60)),0) 
  14. FROM        Structure.Parts pa
  15.  
Feb 4 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Don | last post by:
Hi, I am moving from Sybase to Oracle and I used to be able to do update statement like this in Sybase: UPDATE TABLE1 SET T1.field1 = T2.field2 FROM TABLE1 T1, TABLE2 T2 WHERE T1.field2...
4
by: Hans | last post by:
Hello group, I have a table with the next contents. It lists data about : Who sent what kind of message at what time. For the Level column: The highest level is Critical, the middle is Warning,...
1
by: mirth | last post by:
I would like to update a decimal column in a temporary table based on a set of Glcodes from another table. I search for a set of codes and then want to sum the value for each row matching the...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
1
by: philipdm | last post by:
Ok, I have a data warehouse that I am pulling records from using Oracle SQL. I have a select statement that looks like the one below. Now what I need to do is where the astrics are **** create a...
6
by: ryan.mclean | last post by:
Hi all, first, let me preface this by saying that I am very new to sql server, coming from oracle. Here is my problem: I would like to have a case statement (similar to decode in oracle) that...
14
by: Ryan | last post by:
I want to do the following SQL statement in Access. However, it won't allow me to have the secondary part of my join statement and tells me that this is not supported. OK, so Access doesn't support...
15
by: Hexman | last post by:
Hello All, How do I limit the number of detail records selected in a Master-Detail set using SQL? I want to select all master records for a date, but only the first 3 records for the details...
22
by: pbd22 | last post by:
hi. I am having probelms with an update statement. every time i run it, "every" row updates, not just the one(s) intended. so, here is what i have. i have tried this with both AND and OR and...
4
Zwoker
by: Zwoker | last post by:
Hi all, I'm not sure whether this should be posted in the MS Access forum or one of the SQL forums (which one?), so I'll start here. I'm self taught in the syntax of SQL based queries that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.