Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem displaying Results

Author  Topic 

CheesePlant
Starting Member

5 Posts

Posted - 2011-06-02 : 06:09:29
Hello,

I am very new to SQL (first day!) and I have a problem:


Works but doesn't include consupp.address1 only fields.

Select c1.Accountno, c1.Company, c1.City, c1.Address1, c1.State, c1.Zip, cs.ADDRESS2 as Primary_Contact, cs.CONTSUPREF as Email_Address, cs.Mergecodes as Email_Merge, c1.Mergecodes as Mail_Merge, cs.zip
From contsupp as cs, contact1 as c1
Where (cs.rectype = 'P' and cs.contact = 'E-mail Address') and c1.key1 = 'CUSTOMER' and substring(cs.zip,2,1)='1' and c1.accountno=cs.accountno

Works but no email shown:
SELECT * FROM CONTSUPP INNER JOIN CONTACT1 ON CONTSUPP.ACCOUNTNO=CONTACT1.ACCOUNTNO WHERE (CONTSUPP.RECTYPE = 'P' AND CONTSUPP.CONTACT = 'Enquiry' AND CONTSUPP.Address1 = 'test text')

------

How can I get the 2nd paragraph to display the email address too?

The 1st paragraph shows all the customers results fine but I need to only show ones where consupp.address1 = "test text"?

I'm not bother which code works just as long as it shows the same result :)

Can anyone please help me?

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-06-02 : 06:16:16
Welcome

I moved your join condition from the where clause... and added a condition for the cs.Address1:

Select c1.Accountno, c1.Company, c1.City, c1.Address1, c1.State, c1.Zip, cs.ADDRESS2 as Primary_Contact, cs.CONTSUPREF as Email_Address, cs.Mergecodes as Email_Merge, c1.Mergecodes as Mail_Merge, cs.zip
From contsupp as cs
Inner Join contact1 as c1
On c1.accountno = cs.accountno

Where
(cs.rectype = 'P' and cs.contact = 'E-mail Address')
and c1.key1 = 'CUSTOMER'
and substring(cs.zip,2,1)='1'
and cs.Address1 = 'test text'

quote:
Originally posted by CheesePlant

Hello,

I am very new to SQL (first day!) and I have a problem:


Works but doesn't include consupp.address1 only fields.

Select c1.Accountno, c1.Company, c1.City, c1.Address1, c1.State, c1.Zip, cs.ADDRESS2 as Primary_Contact, cs.CONTSUPREF as Email_Address, cs.Mergecodes as Email_Merge, c1.Mergecodes as Mail_Merge, cs.zip
From contsupp as cs, contact1 as c1
Where (cs.rectype = 'P' and cs.contact = 'E-mail Address') and c1.key1 = 'CUSTOMER' and substring(cs.zip,2,1)='1' and c1.accountno=cs.accountno

Works but no email shown:
SELECT * FROM CONTSUPP INNER JOIN CONTACT1 ON CONTSUPP.ACCOUNTNO=CONTACT1.ACCOUNTNO WHERE (CONTSUPP.RECTYPE = 'P' AND CONTSUPP.CONTACT = 'Enquiry' AND CONTSUPP.Address1 = 'test text')

------

How can I get the 2nd paragraph to display the email address too?

The 1st paragraph shows all the customers results fine but I need to only show ones where consupp.address1 = "test text"?

I'm not bother which code works just as long as it shows the same result :)

Can anyone please help me?




Corey

I Has Returned!!
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-02 : 06:19:35
It looks like you have multiple contact rows in CONTSUPP for an account and you want the one with contact = 'E-mail Address'
but filtering on the address from CONTACT = 'Enquiry'

SELECT cs2.*, c1.*
FROM CONTSUPP cs
JOIN CONTACT1 c1
ON cs.ACCOUNTNO=c1.ACCOUNTNO
join CONTSUPP cs2
ON cs.ACCOUNTNO=cs2.ACCOUNTNO
and cs2.contact = 'E-mail Address'
and cs2.rectype = 'P'
WHERE cs.RECTYPE = 'P'
AND cs.CONTACT = 'Enquiry'
AND cs.Address1 = 'test text'

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

CheesePlant
Starting Member

5 Posts

Posted - 2011-06-02 : 07:23:07
Thanks!

nigelrivett that worked perfectly.

Just expanding a bit, is it possible to add some or statements:

key4 = 'entry1'
OR key4 like '%entry2%'
OR key4 = 'entry3'
OR key4 = 'entry4'

and also factor in some postcodes (zip codes):

contact1.zip like 'BA1%'
OR contact1.zip like 'BA10%'
OR contact1.zip like 'BA11%'

I'm a complete noob :)
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-02 : 07:32:58
Sure - I think these are all on the contact1 table so


SELECT cs2.*, c1.*
FROM CONTSUPP cs
JOIN CONTACT1 c1
ON cs.ACCOUNTNO=c1.ACCOUNTNO
and ( c1.zip like 'BA1%'
OR c1.zip like 'BA10%'
OR c1.zip like 'BA11%'
)
and ( key4 = 'entry1'
OR c1.key4 like '%entry2%'
OR c1.key4 = 'entry3'
OR c1.key4 = 'entry4'
)
join CONTSUPP cs2
ON cs.ACCOUNTNO=cs2.ACCOUNTNO
and cs2.contact = 'E-mail Address'
and cs2.rectype = 'P'
WHERE cs.RECTYPE = 'P'
AND cs.CONTACT = 'Enquiry'
AND cs.Address1 = 'test text'


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

CheesePlant
Starting Member

5 Posts

Posted - 2011-06-02 : 09:52:47
Amazing thank you so much!

This stuff seems so complicated :)
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-02 : 10:29:13
Your CONTSUPP table can be considered as mutiple virtual tables partitioned by the CONTACT value.
We need to use two of these tables - one for 'E-mail Address' and one for 'Enquiry'

If you tghink of them as different tables rather than aliases of the same table it'll probably seem easier.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -