473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

SQL Solution Needed

1
I have 2 tables: CUST and ADDR. First I’ll describe the data and then the query needed.

CUST TABLE

Has PK CUST_ID and FK/composite unique key (CUST_NUM, CONTACT_NUM). A record exists on CUST for each organization (ABC Company) as well as a record for each contact within an organization (Jon Doe “contact at” ABC Company), with each having a unique CUST_ID. Organizations will have a CONTACT_NUM = 0 and may be assigned to one PARENT, which is another record on CUST. PARENT organization records cannot have a PARENT assigned to them and cannot have any associated contacts. A customer contact record cannot have a PARENT.

CUST_ID CUST_NUM CONTACT_NUM PARENT
111 123 0 88888888 org record
222 123 1 0 contact record
333 123 2 0 contact record
444 321 0 44444444 org record
555 123 4 0 contact record
666 567 0 88888888 org record
777 567 1 0 contact record
888 123 6 0 contact record
44444444 0 0 0 parent record
88888888 0 0 0 parent record

ADDR TABLE
Has PK ADDR_ID and a FK/composite NON-unique key (CUST_ID, ADDR_NUM, USE_CD, PREF_CD). A record on ADDR does not represent a unique address, but represents the intersection between customer, address, address use (mailing/billing/shipping), and preferred address status (preferred/not preferred). A customer can only have one preferred address for each address use, but can have multiple non-preferred addresses per USE_CD.

ADDR_ID CUST_ID ADDR_NUM USE_CD PREF_CD
1 222 1 4 1
2 222 2 2 1
3 222 3 4 2
4 333 2 2 2
5 333 9 4 2
6 555 1 3 2
7 777 2 4 1
8 777 3 3 2
9 888 1 4 2
10 888 1 4 2


QUERY
I want to select all customer contact records whose organization is assigned to parent 88888888 (excluding org records) . For each customer contact, I want only 1 address, which I’ll define as “the best” address. To identify the best address, analyze USE_CD and PREF_CD and select the first record in the following prioritized list of conditions:
1) USE_CD = 4 and PREF_CD = 1 (MAILING PREFERRED) one or zero per cust
2) USE_CD = 2 and PREF_CD = 1 (BILLING PREFERRED) one or zero per cust
3) USE_CD = 4 and PREF_CD = 2 (MAILING SECONDARY) multiples possible per cust
4) Any other address record. multiples possible per cust

If a record doesn’t exist for a customer contact that meets condition 1 or 2 above, there may be multiple addresses that meet condition 3, so randomly pull the first record meeting condition 3, and use the same logic for condition for 4 if no records are found that meet condition 3.

Result Set for above data should look as follows:
CUST_ID CUST_NUM CONTACT_NUM ADDR_NUM
222 123 1 1
333 123 2 9
555 123 4 1
777 567 1 2
888 123 6 1

Here is the SQL that I currently have (but the correlated sub-select doesn’t allow the order by clause or the fetch first row clause). I can’t do a min(addr_rank) with a group by, because the addr_num is what is needed and is not unique with cust_id and addr_rank. Any suggestions for correcting this sql or writing something else that works is greatly appreciated.
SELECT B.CUST_ID
,B.CUST_NUM
,B.CONTACT_NUM
,C.ADDR_NUM
FROM CUSTOMER A
,CUSTOMER B
,ADDR C
WHERE A.PARENT IN (88888888)
AND A.CUST_NUM = B.CUST_NUM
AND B.CONTACT_NUM <> 0
AND C.ADDR_NUM IN
(SELECT E.ADDR_NUM
FROM (SELECT F.CUST_ID, F.ADDR_NUM
,CASE WHEN F.USE_CD = 4 AND F.PREF_CD = 1
THEN 1
WHEN F.USE_CD = 2 AND F.PREF_CD = 1
THEN 2
WHEN F.USE_CD = 4 AND F.PREF_CD = 2
THEN 3
ELSE 4
END AS ADDR_RANK
FROM ADDR F
WHERE F.STAT_CD = 1 (active status)
AND F.CUST_ID = B.CUST_ID) E
ORDER BY E.ADDR_RANK
FETCH FIRST ROW ONLY)
FOR FETCH ONLY WITH UR;
Mar 2 '07 #1
1 2293
Snib
24
TC2,

I set the table up in MS Access to try out the SQL so you will need to convert the nested IIF statments into CASE statement but beyond that the SQL is as per the column names you supplied.

The SQL will not cater for the possibility off there being more than 2 addresses for one contact with the same catergory as there is nothing on your tables to distinguish between the rows. It would return multiple rows in this situation.

What you could do is added a timestamp_inserted field to the address table. This could then be used as a further MAX statement on this value within the MAX_ADDRESS sub-table to ensure that you only selected the row with the MAX timestamp_inserted within the ADDRESS_RANK. You would then add a futher predicate to compare the timestamp_inserted on the ADDR_DETAILS sub-table agains the MAX(timestamp_inserted) returned from the MAX_ADDRESS sub-table.

Anyway, this is the SQL I came up with, hope this helps:

select addr_details.cust_num,
addr_details.cust_id,
addr_details.addr_rank,
addr_details.addr_id
from
(SELECT CONTACT.CUST_NUM,
CONTACT.CUST_ID,
iif(USE_CD = 4 AND PREF_CD = 1,1,
IIF(USE_CD = 2 AND PREF_CD = 1, 2,
iif(USE_CD = 4 AND PREF_CD = 2,3,4))) as ADDR_RANK,
ADDRESS.ADDR_ID
FROM CUSTOMER as CUST,
CUSTOMER as CONTACT,
ADDRESS
where CUST.PARENT = 88888888
and CUST.CONTACT_NUM = 0
and CUST.CUST_NUM = CONTACT.CUST_NUM
and CONTACT.CONTACT_NUM > 0
and ADDRESS.CUST_ID = CONTACT.CUST_ID) as ADDR_DETAILS,
(SELECT CONTACT.CUST_NUM,
CONTACT.CUST_ID,
max(iif(USE_CD = 4 AND PREF_CD = 1,1,
IIF(USE_CD = 2 AND PREF_CD = 1, 2,
iif(USE_CD = 4 AND PREF_CD = 2,3,4)))) as ADDR_RANK
FROM CUSTOMER as CUST,
CUSTOMER as CONTACT,
ADDRESS
where CUST.PARENT = 88888888
and CUST.CONTACT_NUM = 0
and CUST.CUST_NUM = CONTACT.CUST_NUM
and CONTACT.CONTACT_NUM > 0
and ADDRESS.CUST_ID = CONTACT.CUST_ID
group by contact.cust_num, contact.cust_id) as MAX_ADDRESS

where addr_details.cust_num = max_address.cust_num
and addr_details.cust_id = max_address.cust_id
and addr_details.addr_rank = max_address.addr_rank

order by 1,2,3
;


Regards

Snib
Mar 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jamie Meredith | last post by:
We are a small graphic design firm who is fighting a growth spurt which has resulted in an overload in our handling of email and proofing. We are looking for a pre-built, open source PHP/MYSQL...
1
by: BJS | last post by:
Sorry for the cross-posting, but based on the number of people I have seen ask for a solution to this problem, I hope by cross-posting this, that it will help a lot of people out of a common...
8
by: M O J O | last post by:
Hi, I'm creating an CRM solution for my company. I want to split up the solution into several classlibraries, so I dont need to build the entire solution every time I run my project. First...
20
by: msa | last post by:
Hi there, First off, let me say that I know that launching to full screen is a bad idea. I would never do it given the choice, but I must follow orders from my boss, the boss that desparately...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
0
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs...
3
by: Bruce W.1 | last post by:
I've been keeping my solution .sln files for ASP.NET projects off in some remote location, along with all my other .sln files. This confuses things and I think it really belongs with the web app...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
12
by: contactmayankjain | last post by:
Hi, Its said that one should avoid dynamic allocation of memory, as it de- fragment the memory into small chunks and allocation of memory is a costly process in terms of efficiency. So what is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.