?
Solved

Replicate field in query output

Posted on 2009-04-13
8
Medium Priority
?
233 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
  • 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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 800 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 1200 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

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
A method of moving multiple mailboxes (in bulk) to another database in an Exchange 2010/2013/2016 environment...
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

584 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