Solved

Query for avoiding multiple rows.

Posted on 2007-03-20
7
207 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 50

Accepted Solution

by:
Lowfatspread earned 250 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

837 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