Solved

simple if-statement

Posted on 2009-07-06
6
182 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

22 Experts available now in Live!

Get 1:1 Help Now