Solved

simple if-statement

Posted on 2009-07-06
6
187 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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 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…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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