Solved

Replicate field in query output

Posted on 2009-04-13
8
221 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 142

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 142

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now