Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simple if-statement

Posted on 2009-07-06
6
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

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

Accepted Solution

by:
pssandhu earned 100 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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