Jump to content

Help with SQL Query, plz


Charlatat

Recommended Posts

I have a complicated query, that I need to make more complicated. The current query is this:

SELECT contact1.contact,contact1.address1,contact1.address2,contact1.city,contact1.state,contact1.zip,conthist.ondate,contact1.key3,conthist.resultcode,conthist.accountno,conthist.userid,conthist.recid FROM conthist INNER JOIN contact1 ON conthist.accountno = contact1.accountno WHERE (conthist.RESULTCODE = 'TNS') AND (contact1.address1 <> '' OR contact1.city <> '' OR contact1.state <> '' OR contact1.zip <> '') AND (ondate = '%targetdate%') AND (contact1.accountno NOT IN (SELECT conthist.accountno FROM conthist WHERE (conthist.resultcode IN ('SLE','SLD','SL1','SL2') AND (conthist.ondate BETWEEN '%targetdate%' AND '%now%'))))"

%targetdate% = '1/1/2007'%now% = automatically generatedNeed to incorporate a third table into this mix (CAL), where cal.rectype = 'A' AND cal.ondate > '%now%'Everytime I try adding the third table, I get fatal errors (even when I don't query it at all). Was wondering if someone could offer a suggestion? Thanks

Link to comment
Share on other sites

Well, it'll fail when I do this:

SELECT contact1.contact,contact1.address1,contact1.address2,contact1.city,contact1.state,contact1.zip,conthist.ondate,contact1.key3,conthist.resultcode,conthist.accountno,conthist.userid,conthist.recid FROM contact1,conthist,calWHERE (contact1.accountno = conthist.accountno AND contact1.accountno = cal.accountno)

it will pretty much fail when the FROM statement has more than 2 tables in it. Thought there was a standard way of joining/querying 3+ tables within a single statement, thus the plea for help.

Link to comment
Share on other sites

You can do multiple INNER JOINS:

SELECT a.field1, a.field2, b.field2, c.field2, c.field3FROM TableA a    INNER JOIN TableB b ON a.field1 = b.field1 INNER JOIN TableC c ON a.field1 = c.field1WHERE b.field4 = 'foo'

Link to comment
Share on other sites

SELECT contact1.contact,contact1.address1,contact1.address2,contact1.city,contact1.state,contact1.zip,conthist.ondate,contact1.key3,conthist.resultcode,conthist.accountno,conthist.userid,conthist.recidFROM contact1 INNER JOIN conthist ON contact1.accountno = conthist.accountno INNER JOIN Cal ON contact1.accountno = Cal.accountnoWHERE (conthist.RESULTCODE = 'TNS') AND (contact1.address1 <> '' OR contact1.city <> '' OR contact1.state <> '' OR contact1.zip <> '') AND (contact1.accountno NOT IN (SELECT conthist.accountno FROM conthist WHERE (conthist.resultcode IN ('SLE','SLD','SL1','SL2'))))

I get a "Table doesn't exist" error, which is the same as when I tried just "FROM contact1,conthist,cal". I know each table does, in fact, exist. Also, these are dbf files. Just in case that makes a difference

Link to comment
Share on other sites

Can you select rows from each of the tables individually or is there possibly a naming issue?dBase Sql isn't my primary sql, but it doesn't seem that you should be getting an error based on the syntax.Here's an alternative showing how you can move the filter conditions on conthist into a sub query and simplify the outer where clause.I'm not sure that i've got your logic correct, might require some tweaking.Mostly I'm wondering about the error, let us know if you're able to select from each of the tables individually.

SELECT contact1.contact, contact1.address1, contact1.address2, contact1.city, contact1.state, contact1.zip	, conthist.ondate, contact1.key3, conthist.resultcode, conthist.accountno, conthist.userid, conthist.recidFROM contact1 INNER JOIN ( SELECT ondate, resultcode, accountno, userid, recid FROM conthist WHERE resultcode NOT IN ('SLE','SLD','SL1','SL2') AND resultcode = 'TNS' AND ondate > GetDate()	) AS conthist ON contact1.accountno = conthist.accountno	INNER JOIN Cal ON contact1.accountno = Cal.accountno AND cal.rectype = 'A' WHERE contact1.address1 <> '' OR contact1.city <> '' OR contact1.state <> '' OR contact1.zip <> ''

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...