![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I was hoping somebody could assist me with this. I need to find accounts that do not contain a contact person called 'Accounting'. Each account may contain multiple contacts. Here's my query to find accounts that DO contain an 'Accounting' contact: SELECT company.code, company.name, company.type, contacts.fullname FROM company INNER JOIN contacts ON company.code = contacts.code WHERE (company.type in ('C', 'R')) and (contacts.fullname = 'Accounting') order by company.code How do I find accounts that DO NOT contain an 'Accounting' contact? |
#3
| |||
| |||
|
|
Artie (artie2269 (AT) yahoo (DOT) com) writes: I was hoping somebody could assist me with this. I need to find accounts that do not contain a contact person called 'Accounting'. Each account may contain multiple contacts. Here's my query to find accounts that DO contain an 'Accounting' contact: SELECT company.code, company.name, company.type, contacts.fullname FROM company INNER JOIN contacts ON company.code = contacts.code WHERE (company.type in ('C', 'R')) and (contacts.fullname = 'Accounting') order by company.code How do I find accounts that DO NOT contain an 'Accounting' contact? SELECT cm.code, cm.name, cm.type, ct.fullname FROM company cm JOIN contacts ct ON cm.code = ct.code WHERE cm.type in ('C', 'R')) AND NOT EXISTS (SELECT * FROM contacts ct2 WHERE cm.code = ct2.code AND ct2.fullname = 'Accounting') ORDER BY cm.code -- Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx |
#4
| |||
| |||
|
|
I need to find accounts that do not contain a contact person called 'Accounting'. Each account may contain multiple contacts. |
#5
| |||
| |||
|
|
Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. I need to find accounts that do not contain a contact person called 'Accounting'. Each account may contain multiple contacts. What is the key of the Companies table (your singular name means that you have one or fewer rows in that table, but I assume this is part of the other violations of ISO-11179 with uselessly vague data element names like "name" (of my dog?) "code" (ZIP code?) and "type" (blood type?). It sorta looks like code is the key, but that makes no sense. BY DEFINITION a code of any kind cannot be a key; this is fundamental. You need a DUNS number or other industry standard company identifier. Your spec asked only for for the companies without a contact = 'Accounting'; but we have no idea if the Contacts table (properly named!) has a reference to the companies. Here is a weird way to do this, based on guessing at your DDL: SELECT CT.company_name FROM Contacts AS CT GROUP BY CT.company_name HAVING SUM(CASE WHEN CT.contact_name = 'Accounting' THEN 1 ELSE 0 END)= 0; This is untested; if we had DDL, we could try it!! I assumed that Contacts ought to be a relationship between a company and a lawful person or role within the company. |
#6
| |||
| |||
|
|
I did not post the full DDL because there are so many fields that are not relevant and felt it would complicate things. It would open up a can of worms as to why it is desgined the way it is (I did not design it). |
|
Do you guys (or gals) have a preferred method of generating insert statements that pull data from the table? I have used sp_generate_inserts from http://vyaskn.tripod.com but have run into some cases where the 8000 char limit was not enough. |
#7
| |||
| |||
|
|
Your spec asked only for for the companies without a contact = 'Accounting'; but we have no idea if the Contacts table (properly named!) has a reference to the companies. |
|
SELECT CT.company_name FROM Contacts AS CT GROUP BY CT.company_name HAVING SUM(CASE WHEN CT.contact_name = 'Accounting' THEN 1 ELSE 0 END)= 0; This is untested; if we had DDL, we could try it!! I assumed that Contacts ought to be a relationship between a company and a lawful person or role within the company. |
#8
| |||
| |||
|
|
--CELKO-- wrote: Your spec asked only for for the companies without a contact = 'Accounting'; but we have no idea if the Contacts table (properly named!) has a reference to the companies. Yes, we do. "It sorta looks like code is the key", by your own admission, and his "companies with a contact = 'Accounting')" sample query backs this up: FROM company INNER JOIN contacts ON company.code = contacts.code Granted, "'code' is a bad name for a key column" is a valid complaint. SELECT CT.company_name FROM Contacts AS CT GROUP BY CT.company_name HAVING SUM(CASE WHEN CT.contact_name = 'Accounting' THEN 1 ELSE 0 END)= 0; This is untested; if we had DDL, we could try it!! I assumed that Contacts ought to be a relationship between a company and a lawful person or role within the company. You mean a many-to-many linking table between Companies and Persons (i.e. a person might be a contact for multiple companies)? Okay, but I still don't see why you would get company_name from any table other than Companies. Basic normalization. If you do get company_name from Companies, then you might have a company with no contacts at all. You could use LEFT JOIN and COALESCE(SUM()), but Erland's NOT EXISTS is more natural. |
.....
#9
| |||
| |||
|
|
Consider this alternative and let me know if I screw this one up, after all, it's getting late, and I'm still at work .....SELECT company.code, company.name, company.type, contacts.fullname FROM company Left JOIN contacts ON company.code = contacts.code AND (company.type in ('C', 'R')) and (contacts.fullname = 'Accounting') WHERE contacts.code IS NULL order by company.code |
#10
| |||
| |||
|
|
creedp... (AT) gmail (DOT) com wrote: Consider this alternative and let me know if I screw this one up, after all, it's getting late, and I'm still at work .....SELECT company.code, company.name, company.type, contacts.fullname FROM company Left JOIN contacts ON company.code = contacts.code AND (company.type in ('C', 'R')) and (contacts.fullname = 'Accounting') WHERE contacts.code IS NULL order by company.code I think this would work (except in the unlikely case that contacts.code is nullable, in which case it might return some data that it shouldn't). But NOT EXISTS has the strong advantage of letting you say what you mean. |
![]() |
| Thread Tools | |
| Display Modes | |
| |