Solved

Replicate field in query output

Posted on 2009-04-13
8
223 Views
Last Modified: 2012-05-06
We have a MySQL database containing contact information for our customers. I was asked to query the database for all contacts for a specific salesman "GGS". (The query is below) The problem I am having is that many facilities have multiple contacts. I have to include the phone number "BusinessPhone" but our system is a little weird on how it saves the data. It seems if there is only 1 contact number saved under BusinessPhone then it only saves the phone number for the first contact but leave the fields blank for all other contacts for that faciltiy. (So if Joe, Sally and John are all contacts with the phone number 555-5555, Joe has the number listed in the database but John and Sally do not) So I need to find a way to replicate the BusinessPhone field to any other contacts for the same facility with no entery. This is for output purposes only, not an update of the database.
SELECT
  T1.CustomerName,
T1.Address1,
T1.Address2,
T1.City,
T1.State,
T1.Zip,
  T.LastName,
  T.FirstName,
  T.JobTitle,
  T.BusinessPhone,
  T.BusinessPhoneExt,
  T.Fax,
  T.CellPhone,
  T.EmailAddress
FROM arcconts T
  Left Outer Join arcusts T1 On T.CustID = T1.CustID
Left Outer Join arsmen T2 On T1.SmanID = T2.SmanID
Where T2.SmanCode = 'GGS'
and T.FirstName <> 'OrderConfirmContact'

Open in new window

0
Comment
Question by:dannyg280
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 

Author Comment

by:dannyg280
ID: 24130913
I should add that "CustomerName" is actually the facility name
0
 
LVL 14

Expert Comment

by:racek
ID: 24131008
replace facility  with address ??? or what you mean with that
SELECT
  T1.CustomerName,
T1.Address1,
T1.Address2,
T1.City,
T1.State,
T1.Zip,
  T.LastName,
  T.FirstName,
  T.JobTitle,
  COALESCE(T.BusinessPhone,tt.bphone) as BusinessPhone
  T.BusinessPhoneExt,
  T.Fax,
  T.CellPhone,
  T.EmailAddress
FROM arcconts T
  Left Outer Join arcusts T1 On T.CustID = T1.CustID
Left Outer Join arsmen T2 On T1.SmanID = T2.SmanID
Left JOIN (SELECT MAX(BusinessPhone) as bphone, facility from arcconts) tt 
   on T.facility = tt.facility 
Where T2.SmanCode = 'GGS'
and T.FirstName <> 'OrderConfirmContact'

Open in new window

0
 
LVL 14

Expert Comment

by:racek
ID: 24131013
missing ,
SELECT
  T1.CustomerName,
T1.Address1,
T1.Address2,
T1.City,
T1.State,
T1.Zip,
  T.LastName,
  T.FirstName,
  T.JobTitle,
  COALESCE(T.BusinessPhone,tt.bphone) as BusinessPhone,
  T.BusinessPhoneExt,
  T.Fax,
  T.CellPhone,
  T.EmailAddress
FROM arcconts T
  Left Outer Join arcusts T1 On T.CustID = T1.CustID
Left Outer Join arsmen T2 On T1.SmanID = T2.SmanID
Left JOIN (SELECT MAX(BusinessPhone) as bphone, facility from arcconts) tt 
   on T.facility = tt.facility 
Where T2.SmanCode = 'GGS'
and T.FirstName <> 'OrderConfirmContact'

Open in new window

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
ID: 24131102
note:

Left Outer Join arsmen T2 On T1.SmanID = T2.SmanID
...
Where T2.SmanCode = 'GGS'

will make the left join implicitly a inner join, aka:

inner Join arsmen T2 On T1.SmanID = T2.SmanID
..
Where T2.SmanCode = 'GGS'


if you want to maintain the left join effects, change to :

Left Outer Join arsmen T2 On T1.SmanID = T2.SmanID and T2.SmanCode = 'GGS'
...

regards
0
 
LVL 14

Accepted Solution

by:
racek earned 300 total points
ID: 24131146
with angelIII suggestion and missing 'group by'

SELECT
  T1.CustomerName,
T1.Address1,
T1.Address2,
T1.City,
T1.State,
T1.Zip,
  T.LastName,
  T.FirstName,
  T.JobTitle,
  COALESCE(T.BusinessPhone,tt.bphone) as BusinessPhone,
  T.BusinessPhoneExt,
  T.Fax,
  T.CellPhone,
  T.EmailAddress
FROM arcconts T
  Left Outer Join arcusts T1 On T.CustID = T1.CustID
Left Outer Join arsmen T2 On T1.SmanID = T2.SmanID and T2.SmanCode = 'GGS'
Left JOIN (SELECT MAX(BusinessPhone) as bphone, facility from arcconts group by 2) tt
   on T.facility = tt.facility
Where  
   T.FirstName <> 'OrderConfirmContact'
0
 

Author Comment

by:dannyg280
ID: 24131228
OK...thanks for the responces. I am getting an error now, and I'm not sure if it is a simple syntext error or if it is a limitation of the Query module in our LOB app... but I'm getting a "SQL error - Table name expected after JOIN keyword, instead found '('

Maybe the module doesn't allow a nested select inside a Join statement..
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24133237
what version of MySQL are you using?
0
 

Author Closing Comment

by:dannyg280
ID: 31569564
I found out it was a limitation of the query "module" in our LOB app. That you both for your help.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question