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.
Author |
Topic |
NiceSituation
Starting Member
22 Posts |
Posted - 2008-10-24 : 16:22:09
|
Hi everyone, I have a problem trying to extract customer, agency, contact and sales representatives information and posting it several times on different fields. I have two tables, A and B. A contains the following fields: Id, Name (which includes the name for all customers, agencies, contacts and sales representatives), Salesrep, Agency, Contact. B contains the following fields: Id, Organization_Number, Account_Number. A.Salesrep, A.Agency and A.Contact hold the same Id as A.Id, and since A.Name refers to all types of "people", this means that for a customer, A.Salesrep gives you the Id (A.Id) of his sales representative, which also applies to A.Agency and A.Contact for the agency through which the customer makes contact with the organization and the contact person on the customer's side, respectively. For a Sales Representative, all three fields will be left NULL, for an Agency A.Salesrep and A.Contact may or may not be NULL, while A.Agency will be NULL, and for a Contact A.Salesrep may or may not be NULL and the other two will be NULL. Table B holds more information about table A's "people", and through their Ids, both tables can be joined.The main problem came when I was asked to return all customers, agencies and contacts and their respective sales representatives, agencies and contacts (their names, of course). I need to return these four fields (A.Name, A.Salesrep, A.Agency, A.Contact) as well as B.Organization_Number and B.Account_Number.Currently, I have the following query:SELECT DISTINCT TOP 100 PERCENT B.Organization_Number, B.Account_Number, A.Name, --customer, agency, sales representative or contact names ALIAS1.Name AS Sales_rep, --sales representative for this person ALIAS2.Name AS Agency, --agency for this person ALIAS3.Name AS Contact, --contact for this person FROM A INNER JOIN B ON A.Id = B.Id INNER JOIN A ALIAS1 ON A.Salesrep = ALIAS1.Salesrep INNER JOIN A ALIAS2 ON A.Agency = ALIAS2.Agency INNER JOIN A CUENTA3 ON A.Contact = ALIAS3.Contact ORDER BY B.Account_NumberHowever, this returns for each "person" all combinations of A.Salesrep, A.Agency and A.Contact, ignoring those in which any of those fields is NULL. Instead of having a single table with one row per "person", I get loads of rows per person, and only for those where all fields are not NULL.Any thoughts as to how I can only return the information required? If you need additional information, please let me know. May today be good, and tomorrow better |
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-24 : 16:32:17
|
Hi, show us your original Tables and what output result should look like. |
|
|
NiceSituation
Starting Member
22 Posts |
Posted - 2008-10-24 : 19:30:01
|
quote: Originally posted by hanbingl Hi, show us your original Tables and what output result should look like.
Hi, here is a sample of some rows from the original tables:Table AId Salesrep Name Contact Agency0010 NULL Sales Team 1 NULL NULL 1037 0010 ABBOTT LABS 1038 01071053 0013 ACTINVER NULL NULL0965 NULL ADCON 0078 NULL0039 0010 NORTH BANK 1045 01060998 NULL MONEX BANK NULL 0010 0034 0010 NAT BANK 0939 00980330 0010 BARTHER M NULL 00950053 0010 BIDDY MULIGAN 0941 01060448 0013 BMW CORP. 0900 NULL1044 0399 SAT. DISHES 1240 NULL0006 NULL Lauren Moss NULL NULL0013 NULL Josh Sanz NULL NULL0596 NULL MGMT. CENTER 1908 09301038 NULL TIM KRAFT NULL NULL0107 NULL MIND SHARE 0208 NULL0900 NULL ROGER SMITH NULL NULLTable BId Org_Number Account_Number0006 NULL E50010 NULL E90011 NULL E100013 NULL E120034 BNM840515VB1 4001600140053 BBA830831LJ2 4001600160488 88888880200107 MSM981208FQ5 2001800121038 NULL 88888881971037 ALM84030811A 99999991001038 NULL 88888881971042 NULL 88888882091043 NULL 88888882031044 CSA0702081Z2 4001301541045 NULL 88888881991046 NULL 88888882011052 NULL NULL1053 NULL 88888882121203 MWX0212129Q7 NULL1204 GGP07100938A NULLOutput result should look like this:Name Salesrep Contact Agency Org_Number Account_NumberSales Team 1 NULL NULL NULL NULL E9ABBOTT LABS Sales Team 1 TIM KRAFT MIND SHARE ALM84030811A 9999999100BMW CORP. Josh Sanz ROGER SMITH NULL NULL 8888888020And so on.The idea is to sort of paste both tables side to side, having as the paste condition the coincidence of Id on both of them (A.Id = B.Id), in such a way that instead of having the Ids of the sales representatives, agencies and contacts, I get their names, which are mixed up along with the customer names on table A. This way you get the customer, your sales representative (if anyone specific) in charge of his account, his account and organization number (if they have one), the agency that brought him to you (if an agency brought that customer), and the person (if anyone specific) you should get in touch with for anything that has to do with this customer.May today be good, and tomorrow better |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
NiceSituation
Starting Member
22 Posts |
Posted - 2008-10-27 : 14:55:39
|
Thanks, my problem is solved now.May today be good, and tomorrow better |
|
|
|
|
|
|
|