Solved

simple if-statement

Posted on 2009-07-06
6
185 Views
Last Modified: 2012-05-07
Hi, I want to create a new field called "total_Qty_sold", that uses an If-statement, that sums the field called "TOTAL_SHIPPED_QTY",  but if the shipped qty is zero ("0"), then it should be replaced by the field called "ORDER_QTY", only if the field called "STATUS" is like "R".

 Please give me the complete SQL for me to copy and paste, based on the attached code snippet.
 thank you very much
SELECT [1].PART_ID, [1].STATUS, [1].TOTAL_SHIPPED_QTY, [1].ORDER_QTY
FROM qryLUPSTCtotalSls_01 AS 1
WHERE ((([1].TOTAL_SHIPPED_QTY)>0)) OR ((([1].STATUS)="R") AND (([1].TOTAL_SHIPPED_QTY)=0));

Open in new window

0
Comment
Question by:Osvel_SQL
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24787986
Something like this may be:

SELECT [1].PART_ID, [1].STATUS, [1].TOTAL_SHIPPED_QTY, [1].ORDER_QTY,SUM(IIF([1].TOTAL_SHIPPED_QTY = 0, IIF([1].STATUS="R", [1].ORDER_QTY,0), [1].TOTAL_SHIPPED_QTY))
FROM qryLUPSTCtotalSls_01 AS 1
WHERE ((([1].TOTAL_SHIPPED_QTY)>0)) OR ((([1].STATUS)="R") AND (([1].TOTAL_SHIPPED_QTY)=0));

Open in new window

0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 24788448
This uses CASE instead of IIF

SELECT [1].PART_ID, [1].STATUS,
      [1].TOTAL_SHIPPED_QTY,
      [1].ORDER_QTY,
      total_Qty_sold = CASE WHEN [1].TOTAL_SHIPPED_QTY = 0 AND [1].STATUS = 'R' THEN [1].ORDER_QTY
                              ELSE [1].TOTAL_SHIPPED_QTY
                              END
FROM qryLUPSTCtotalSls_01 AS 1
WHERE ((([1].TOTAL_SHIPPED_QTY)>0)) OR ((([1].STATUS)="R") AND (([1].TOTAL_SHIPPED_QTY)=0))
0
 

Author Comment

by:Osvel_SQL
ID: 24789026
pls see the attachment for screenshot of error message
Outlook.jpg
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 17

Expert Comment

by:pssandhu
ID: 24789053
Osvel_SQL, did you try my suggestions?
0
 
LVL 17

Accepted Solution

by:
pssandhu earned 25 total points
ID: 24789079
Correction to my syntax, took the sum function out since you do not need it:

SELECT [1].PART_ID, [1].STATUS, [1].TOTAL_SHIPPED_QTY, [1].ORDER_QTY,IIF([1].TOTAL_SHIPPED_QTY = 0, IIF([1].STATUS="R", [1].ORDER_QTY,0), [1].TOTAL_SHIPPED_QTY)
FROM qryLUPSTCtotalSls_01 AS 1
WHERE ((([1].TOTAL_SHIPPED_QTY)>0)) OR ((([1].STATUS)="R") AND (([1].TOTAL_SHIPPED_QTY)=0));

Open in new window

0
 

Author Comment

by:Osvel_SQL
ID: 24789521
I dont think that it is summing correctly, bec I am getting mutliple records for the same customer_ID, with diffrent figures . please advise

SELECT DISTINCT [1].PART_ID, [1].CUSTOMER_ID, IIf([1].[TOTAL_SHIPPED_QTY]=0,IIf([1].[STATUS]="R",[1].[ORDER_QTY],0),[1].[TOTAL_SHIPPED_QTY]) AS total_Qty_sold, [1].TOTAL_SHIPPED_QTY, [1].ORDER_QTY
FROM qryLUPSTCtotalSls_01 AS 1;
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select only the top record in a left join 13 45
Oracle - Query link database loop 8 41
average of calculation (TSQL) 4 26
Sum particular field in database 2 16
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…

830 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