Solved

if-statement for customer bill-to address  different than regular  address

Posted on 2008-10-10
6
153 Views
Last Modified: 2010-03-20
Hi, my custómer table has the bill-to address null, whenever the customer's bill-to address is the same as the regular adress.  please give me the complete SQL that lists the customer bill-to adress, but if the customer doesn't have a bill-to address, then I want it to default to the regular address.
my criteria:
1.  if C.BILL_TO_ADDR_1 is null, then I want to list ADDR_1
2.  if C.BILL_TO_ADDR_1 is null, then I want it to list ADDR_2, if it is not null, then I wan to list C.BILL_TO_ADDR_2
3.  if C.BILL_TO_ADDR_1 is null, then I want it to list ADDR_3, if it is not null, then I wanto list C.BILL_TO_ADDR_3
4.  if C.BILL_TO_ADDR_1 is null, then I want it to list C.CITY, if it is not null, then I wanto list C.BILL_TO_CITY
5.  if C.BILL_TO_ADDR_1 is null, then I want it to list C.STATE, if it is not null, then I wanto list C.BILL_TO_STATE
6.  if C.BILL_TO_ADDR_1 is null, then I want it to list C.COUNTRY, if it is not null, then I wanto list C.BILL_TO_COUNTRY


please give me the complete sql, for me to copy/paste. (I dont know SQL) thank you very much

SELECT C.ID AS customer_ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.COUNTRY, C.BILL_TO_NAME, C.BILL_TO_ADDR_1, C.BILL_TO_ADDR_2, C.BILL_TO_CITY, C.BILL_TO_STATE, C.BILL_TO_COUNTRY
FROM SYSADM_CUSTOMER AS C
GROUP BY C.ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.ZIPCODE, C.COUNTRY, C.BILL_TO_NAME, C.BILL_TO_ADDR_1, C.BILL_TO_ADDR_2, C.BILL_TO_CITY, C.BILL_TO_STATE, C.BILL_TO_COUNTRY;

Open in new window

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

Expert Comment

by:Daniel Reynolds
ID: 22690368
SELECT C.ID AS customer_ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.COUNTRY, coalesce(C.BILL_TO_NAME,C.NAME) [BILL_TO_NAME], coalesce(C.BILL_TO_ADDR_1,C.ADDR_1) [BILL_TO_ADDR_1]
, coalesce(C.BILL_TO_ADDR_2,C.ADDR_2) [BILL_TO_ADDR_2]
, coalesce(C.BILL_TO_CITY,C.ADDR_2) [BILL_TO_CITY]
, coalesce(C.BILL_TO_STATE,C.ADDR_2) [BILL_TO_STATE]
, coalesce(C.BILL_TO_COUNTRY,C.ADDR_2) [BILL_TO_COUNTRY]
FROM SYSADM_CUSTOMER AS C
GROUP BY C.ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.ZIPCODE, C.COUNTRY, C.BILL_TO_NAME, C.BILL_TO_ADDR_1, C.BILL_TO_ADDR_2, C.BILL_TO_CITY, C.BILL_TO_STATE, C.BILL_TO_COUNTRY;
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 22690380
oops, in my hurry, I forgot to update the 2nd value in the last 3 items. they should be

, coalesce(C.BILL_TO_CITY,C.CITY) [BILL_TO_CITY]
, coalesce(C.BILL_TO_STATE,C.STATE) [BILL_TO_STATE]
, coalesce(C.BILL_TO_COUNTRY,C.COUNTRY) [BILL_TO_COUNTRY]
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22690518
Although I would consider it an anomaly, your solution could provide inconsistent data of the address2 field would ever be null for the bill but not the general customer address.

So if the customers address (addr1, addr2, city, state and country) all have values, but the BILL to address has only addr1, city state and country, you would get addr2 from the customer table as if it were part of the billing address.

To get around this, it should be implemented with a case statement (or iif for access).

Make sense:

Test these results.
SELECT C.ID AS customer_ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.COUNTRY
, iif(c.bill_to_name is null, c.name, c.bill_to_name) [bill_to_name]
, iif(c.bill_to_addr_1 is null, c.addr_1, c.bill_to_addr1) [bill_to_addr1]
 
, iif(c.bill_to_addr_1 is null, c.ADDR_2, c.bill_to_addr2) [bill_to_addr1]
, iif(c.bill_to_addr_1 is null, c.CITY, c.BILL_TO_CITY) [bill_to_addr1]
, iif(c.bill_to_addr_1 is null, c.STATE, c.BILL_TO_STATE) [bill_to_addr1]
, iif(c.bill_to_addr_1 is null, c.COUNTRY, c.BILL_TO_COUNTRY) [bill_to_addr1]
 
FROM SYSADM_CUSTOMER AS C
GROUP BY C.ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.ZIPCODE, C.COUNTRY, C.BILL_TO_NAME, C.BILL_TO_ADDR_1, C.BILL_TO_ADDR_2, C.BILL_TO_CITY, C.BILL_TO_STATE, C.BILL_TO_COUNTRY;

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:gringotani
ID: 22691427
Brandon, I tried ur query , but got the following error messsage:Syntax error (missing operator) in query expression 'IIF(C.BILL_TO_NAME IS NULL, C.NAME,C.BILL_TO_NAME) [BILL_TO_NAME'.]

pls advise, thanks
0
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 50 total points
ID: 22691562
remove the '. from teh [BILL_TO_NAME'.]  so it is [BILL_TO_NAME]
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22694100
You have this tagged as access.  Are you running it in access, or as a passthrough query to a different db engine such as sql server?
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

17 Experts available now in Live!

Get 1:1 Help Now