Solved

Replicate field in query output

Posted on 2009-04-13
8
218 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
 
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle - Create Procedure with Paramater 16 56
Oracle Pivot Question 8 43
MySQL - Restore Database SQL File 5 41
Alter an update query which rounds 7 29
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

943 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

6 Experts available now in Live!

Get 1:1 Help Now