Solved

Replicate field in query output

Posted on 2009-04-13
8
216 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

746 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

15 Experts available now in Live!

Get 1:1 Help Now