Solved

simple if-statement

Posted on 2009-07-06
6
186 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

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 …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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