Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query to collate list of doctors

Author  Topic 

rodnew75
Starting Member

8 Posts

Posted - 2010-12-15 : 18:08:42
I have a table with the names of GP and what clinic they work in. It's layout is:
ID
First Name
Last Name
Clinic
Address

I also have a table with the clinic details
Clinic ID
Clinic Name
Phone Number
Fax Number

The number of GP's at a particular clinic varies from 1 to 30.
I am wanting to generate a query that will provide a clinic name and the GP's who work at that clinic. Have tried a number of different statements but find it difficult.

Hopefully the outcome will generate something like:
Clinic Name, GP1, GP2, GP3 etc. on the same row.

thanks for any suggestions or help you can provide. I am working on Openoffice Base.

Rod

ConradK
Posting Yak Master

140 Posts

Posted - 2010-12-15 : 18:52:23
Databases don't normally work that way, because of normalization. It it will only return

ClinicA Doctor 1
ClinicA Doctor 2
ClinicB Doctor 1
ClinicB Doctor 3

If what you are looking for is

ClinicA Doctor 1 Doctor 2
ClinicB Doctor 1 Doctor 3

That's not going to happen unless you write some really fancy code involving row_number and a bunch of sub-queries and aggregate functions.

If you give some sample data I may be able to help you. But the short answer is: it doesn't work that way.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-15 : 19:23:15
Take a look at PIVOT.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rodnew75
Starting Member

8 Posts

Posted - 2010-12-15 : 19:58:10
I have developed a query with the outcome you have mentioned Conrad. The output I get is ok just trying to improve it. I will have a look at Pivot tables and see what I come up with. Thanks for your help
Go to Top of Page

rodnew75
Starting Member

8 Posts

Posted - 2010-12-15 : 20:28:58
Actually, is there a better way to set up the tables to get the desired outcome. One outcome is for a list of doctors surgeries with contact details, the other outcome is a mailing list for the doctors - ie. doctors name, clinic, address
Go to Top of Page

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2010-12-15 : 23:55:06
do you need table design?
Go to Top of Page

rodnew75
Starting Member

8 Posts

Posted - 2010-12-16 : 00:01:13
Yes, I suppose I do. Since it is too difficult to develop a query I am thinking that I may be able to fix my actual design
Go to Top of Page

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2010-12-16 : 00:40:33
mainatain seperate tables for:Doctors,Clinics,Address,Surgeries.because in your scenario it is one to many relation.so normalizing the tables would solve your problem.Still if you did'nt get me you can ask.
Go to Top of Page

jessiefun
Starting Member

35 Posts

Posted - 2010-12-16 : 01:51:53
Acually this design is pretty good. And what you need is a inner join and group by. :)
Go to Top of Page

rodnew75
Starting Member

8 Posts

Posted - 2010-12-16 : 02:10:47
Sorry Ahmed, you have lost me. Do you mean have a doctors name only table with a key, clinic name (with phone numbers) and a key, address details with a key and then associate them? And Jessie, do you mean my design is good or the one ahmed described?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-16 : 02:22:21
Your design is ok so far.
Your problem is a "display-problem" only and should be handled in the front end.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jessiefun
Starting Member

35 Posts

Posted - 2010-12-16 : 04:11:54
Yeah, i prefer to your design
Go to Top of Page

rodnew75
Starting Member

8 Posts

Posted - 2010-12-16 : 20:35:47
The question I have now is regarding how to use the group by clause when dealing with letters. The examples I find always seem to include integers and a sum function
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-16 : 23:20:14
There is not difference in the group by with character data as compared to integers. Please post sample data if you need help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rodnew75
Starting Member

8 Posts

Posted - 2010-12-17 : 00:09:10
ClinicName GPFullname
Amberley Park Padma
Amberley Park Xiao
Amberley Park Ashok
Berwick Family Les
Berwick Family Caroli

This is a table I produce using a query. I am wanting to then group each GP into one cell per clinic. There are about 30 clinics, and at least 190 doctors. So the output I am hoping to get is:
ClinicName GP
Amberley Park Padma, Xiao, Ashok
Berwick Family Les, Janet, Caroline
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-17 : 13:17:32
Look into PIVOT in Books Online.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rodnew75
Starting Member

8 Posts

Posted - 2010-12-18 : 01:55:58
I have looked at PIVOT but don't understand how this works with text. Is someone able to provide a sample or example code on how to configure SQL for a pivot table of similar format to what I have described.
Go to Top of Page
   

- Advertisement -