Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query for avoiding multiple rows.

Posted on 2007-03-20
7
Medium Priority
?
214 Views
Last Modified: 2013-11-30
System: SQL Enterprise Manager v 8.0

I'm trying to make a query that will grab all the "board_specialty" from the physicians tables.  The problem is in the way the tables are designed.  This is how it looks:

Board_Name                            Specialty_Name                  Order_
American Board of...               Surgery                                1
American Board of...               Family Practice                     2
American Board of...               Internal Medicine                  3

How I'd like it to look after the query (to avoid 3 rows with the same info):

Physician_Name        Board_Name1               Board_Name2                Board_Name3               Spec_Name
John Doe                   American board of...     American board of...      American Board of...    Surgery
Mary Sue                   American board of...     American board of...     American board of...     Pediatrics
Bob Dillon                   American board of...     American board of...     American board of...     Cardiovascular

I dont need to query the multiple secialty names...just the different board names on a flat file.

Thanks!
0
Comment
Question by:rortiz77
[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
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18756590
These pivot-type queries are a little rigorous, as you've figured out.  The main difficulty is a dynamic number of columns.

If you have a MAXIMUM number of boards on which a physician may be, you can do something like this:

SELECT B1.Physician_Name, B1.Board_Name as Board_Name1, B2.Board_Name as Board_Name2,
  ... Bn.Board_Name as Board_NameN, B1.Specialy_Name as Spec_Name
From
  Board_Specialty B1 Left Join
  Board_Specialty B2 on B1.Physician_Name = B2.Phsyician_Name And B1.Order_ = 1 And IsNull(B2.Order,2) = 2
... Left Join Board_Specialty Bn on B1.Physician_Name = Bn.Phsyician_Name And  IsNull(Bn.Order,n) = n

Honestly, I think your table structure is a bit more complex than that. If this query doesn't get you there, please tell us about the related tables.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18756868
SELECT B1.Physician_Name, coalesce(B1.Board_Name,'') as Board_Name1, coalesce(B2.Board_Name,'') as Board_Name2,
   coalesce(B3.Board_Name,'') as Board_Name3, B1.Specialy_Name as Spec_Name
From  Board_Specialty B1
Left outer Join   Board_Specialty B2
on B1.Physician_Name = B2.Phsyician_Name
And B2.Order = 2
 Left Outer Join Board_Specialty B3
 on B1.Physician_Name = B3.Phsyician_Name
And  B3.Order = 3
Where B1.Order = 1


0
 

Author Comment

by:rortiz77
ID: 18780222
True, there are more details.  I over simplified the query.  The query right now is made up of 4 tables.  

Med_Prof_Spec:
-Board_name

Med_Prof:
-Last_name
-First_name
-Middle_initial
-Degree
-Languages_spoken

Med_Prof_Offices:
-Address
-City
-Zip
-Phone

Med_Prof_Facilities:
-Expertise

Here is the current query:

SELECT      
                    dbo.Med_Prof.Last_name,
                    dbo.Med_Prof.First_name,
                    dbo.Med_Prof.Middle_initial,
                    dbo.Med_Prof.Degree,
                    dbo.Med_Prof_Offices.Phone_number_1,
                    dbo.Med_Prof_Offices.Fax_number,
                    dbo.Med_Prof_Facilities.Expertise,
                    dbo.Med_Prof_Offices.Address_1,
                    dbo.Med_Prof_Offices.Address_2,
                    dbo.Med_Prof_Offices.City,
                    dbo.Med_Prof_Offices.State,
                    dbo.Med_Prof_Offices.Zip_Code,
                    dbo.Med_Prof.Languages_spoken,
                    dbo.Med_Prof_Specialties.Board_name
FROM         dbo.Med_Prof INNER JOIN
                   dbo.Med_Prof_Offices ON dbo.Med_Prof.Med_Prof_Record_No =    
                   dbo.Med_Prof_Offices.Med_Prof_Record_No INNER JOIN
                   dbo.Med_Prof_Facilities ON dbo.Med_Prof.Med_Prof_Record_No =
                   dbo.Med_Prof_Facilities.Med_Prof_Record_No INNER JOIN
                   dbo.Med_Prof_Specialties ON dbo.Med_Prof.Med_Prof_Record_No =
                   dbo.Med_Prof_Specialties.Med_Prof_Record_No AND
                   dbo.Med_Prof_Facilities.Order_ =
                   dbo.Med_Prof_Specialties.Order_ AND dbo.Med_Prof_Offices.Order_ =
                   dbo.Med_Prof_Specialties.Order_

WHERE    
                    (dbo.Med_Prof_Offices.Primary_office = 1) AND
                    (dbo.Med_Prof_Facilities.Current_status = 'fellow' OR
                     dbo.Med_Prof_Facilities.Current_status = 'active'
                    (dbo.Med_Prof_Facilities.FacCode = 'fhorland01') AND
                    (dbo.Med_Prof.Degree = 'md' OR
                    dbo.Med_Prof.Degree = 'DDS MD' OR
                    dbo.Med_Prof.Degree = 'dds, md' OR
                    dbo.Med_Prof.Degree = 'md, dds'

ORDER BY
                    dbo.Med_Prof.Last_name,
                    dbo.Med_Prof.First_name
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

Accepted Solution

by:
Lowfatspread earned 1000 total points
ID: 18786095
then try this...

Create View vwMedProfBoard
as
Select MPO.Med_prof_record_no     ,              
                    MPO.Phone_number_1,
                    MPO.Fax_number,
                    MPF.Expertise,
                    MPO.Address_1,
                    MPO.Address_2,
                    MPO.City,
                    MPO.State,
                    MPO.Zip_Code,
                    MP.Languages_spoken,
                    MPS.Board_name,mps_order

From  dbo.Med_Prof_Offices as MPO
INNER JOIN   dbo.Med_Prof_Facilities as MPF
   ON MP.Med_Prof_Record_No = MPF.Med_Prof_Record_No
INNER JOIN   dbo.Med_Prof_Specialties as MPS
   ON MP.Med_Prof_Record_No = MPS.Med_Prof_Record_No
  AND MPF.Order_ = MPS.Order_
  AND MPO.Order_ = MPS.Order_

WHERE   MPO.Primary_office = 1
  AND   MPF.Current_status in ('fellow','active')
  AND   MPF.FacCode = 'fhorland01'
  AND   MP.Degree in ('md','DDS MD' ,'dds, md' , 'md, dds' )



Then
SELECT      
                    MP.Last_name,
                    MP.First_name,
                    MP.Middle_initial,
                    MP.Degree,
                    B1.Phone_number_1, b1.Fax_number, b1.Expertise,  b1.Address_1,  b1.Address_2, b1.City,
                b1.State, b1.Zip_Code, b1.Languages_spoken,
                b1.Board_name,
                b2.Board_name,
                b3.Board_name

FROM         dbo.Med_Prof as MP                    
Inner Join vwMedProfBoard as B1
  on mp.med_prof_record_no=b1.med_prof_record_no
left outer join  vwMedProfBoard as B2
  on mp.med_prof_record_no=b2.med_prof_record_no
 and b2.mps_order > b1.order
left outer join  vwMedProfBoard as B3
  on mp.med_prof_record_no=b3.med_prof_record_no
 and b3.mps_order > b2.order
 
ORDER BY  MP.Last_name, MP.First_name
0
 

Author Comment

by:rortiz77
ID: 18792845
Server: Msg 156, Level 15, State 1, Procedure vwMedProfBoard, Line 31
Incorrect syntax near the keyword 'THEN'.
Server: Msg 156, Level 15, State 1, Procedure vwMedProfBoard, Line 56
Incorrect syntax near the keyword 'order'.
0
 

Author Comment

by:rortiz77
ID: 18795178
What is causing this error?
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18817266
LowFatSpread's answer is a view, then a query of that view.  If you want to execute the creation of the view at the same time as the query, replace that middle THEN with GO.

(btw, the solution's his, I'm just helping out a bit.)

The mod I suggest above makes it look like this:


Create View vwMedProfBoard
as
Select MPO.Med_prof_record_no     ,              
                    MPO.Phone_number_1,
                    MPO.Fax_number,
                    MPF.Expertise,
                    MPO.Address_1,
                    MPO.Address_2,
                    MPO.City,
                    MPO.State,
                    MPO.Zip_Code,
                    MP.Languages_spoken,
                    MPS.Board_name,mps_order

From  dbo.Med_Prof_Offices as MPO
INNER JOIN   dbo.Med_Prof_Facilities as MPF
   ON MP.Med_Prof_Record_No = MPF.Med_Prof_Record_No
INNER JOIN   dbo.Med_Prof_Specialties as MPS
   ON MP.Med_Prof_Record_No = MPS.Med_Prof_Record_No
  AND MPF.Order_ = MPS.Order_
  AND MPO.Order_ = MPS.Order_

WHERE   MPO.Primary_office = 1
  AND   MPF.Current_status in ('fellow','active')
  AND   MPF.FacCode = 'fhorland01'
  AND   MP.Degree in ('md','DDS MD' ,'dds, md' , 'md, dds' )



GO --here's the change
SELECT      
                    MP.Last_name,
                    MP.First_name,
                    MP.Middle_initial,
                    MP.Degree,
                    B1.Phone_number_1, b1.Fax_number, b1.Expertise,  b1.Address_1,  b1.Address_2, b1.City,
                b1.State, b1.Zip_Code, b1.Languages_spoken,
                b1.Board_name,
                b2.Board_name,
                b3.Board_name

FROM         dbo.Med_Prof as MP                    
Inner Join vwMedProfBoard as B1
  on mp.med_prof_record_no=b1.med_prof_record_no
left outer join  vwMedProfBoard as B2
  on mp.med_prof_record_no=b2.med_prof_record_no
 and b2.mps_order > b1.order
left outer join  vwMedProfBoard as B3
  on mp.med_prof_record_no=b3.med_prof_record_no
 and b3.mps_order > b2.order
 
ORDER BY  MP.Last_name, MP.First_name
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

610 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