Uprightguy
Programmer
Is there a better way to do this Query?
I have a large database of invoices. These invoices are not grouped by the customer and all of them have 4 fields of phone numbers. The phone numbers for a particular customer are not always the same but they will always have at least 1 phone number in the invoice that will tie it to the other invoices. So even though an invoice only has 4 phone numbers the customer may have 10 or more phone numbers linked to his account.
The purpose of the query is to get all phone # that a customer has ever used in an invoice.
the query starts off with 1 invoice and pulls that invoice into a temp file that has the 4 phone numbers. it then queries the main table and gets all invoices that have those 4 phone numbers. I figured the best way to get all the numbers is to do this query in a loop continually until no further results.
I have a large database of invoices. These invoices are not grouped by the customer and all of them have 4 fields of phone numbers. The phone numbers for a particular customer are not always the same but they will always have at least 1 phone number in the invoice that will tie it to the other invoices. So even though an invoice only has 4 phone numbers the customer may have 10 or more phone numbers linked to his account.
The purpose of the query is to get all phone # that a customer has ever used in an invoice.
the query starts off with 1 invoice and pulls that invoice into a temp file that has the 4 phone numbers. it then queries the main table and gets all invoices that have those 4 phone numbers. I figured the best way to get all the numbers is to do this query in a loop continually until no further results.
Code:
SELECT Cust_phone,Sec_phone,Fax,Text_Num
into #N_Orders_tmp1
FROM <Database>.<DBO>.<Table>
WHERE <Table>.orderID=1236666
Select Cust_phone,Sec_phone,Fax,Text_Num
into #N_Orders_tmp2
FROM <Database>.<DBO>.<Table>
WHERE <Table>.cust_phone in( select cust_phone from #N_Orders_tmp1) or
<Table>.cust_phone in( select Sec_phone from #N_Orders_tmp1) or
<Table>.cust_phone in( select fax from #N_Orders_tmp1) or
<Table>.cust_phone in( select Text_num from #N_Orders_tmp1) or
<Table>.sec_phone in( select cust_phone from #N_Orders_tmp1) or
<Table>.sec_phone in( select Sec_phone from #N_Orders_tmp1) or
<Table>.sec_phone in( select fax from #N_Orders_tmp1) or
<Table>.sec_phone in( select Text_num from #N_Orders_tmp1) or
<Table>.fax in( select cust_phone from #N_Orders_tmp1) or
<Table>.fax in( select Sec_phone from #N_Orders_tmp1) or
<Table>.fax in( select fax from #N_Orders_tmp1) or
<Table>.fax in( select Text_num from #N_Orders_tmp1) or
<Table>.text_num in( select cust_phone from #N_Orders_tmp1) or
<Table>.text_num in( select Sec_phone from #N_Orders_tmp1) or
<Table>.text_num in( select fax from #N_Orders_tmp1) or
<Table>.text_num in( select Text_num from #N_Orders_tmp1)