471,090 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,090 software developers and data experts.

help in sql query


the relations are

MEMBERSHIP(membership_number (pk), cover_type(pk),
start_date(pk), end_date)

PERSON(person_number(pk), surname, initials, street_address, suburb, ,
membership_number)

DOCTOR(doctor_number(pk), surname, initials, street_address, suburb)

PROVIDED-SERVICE(person_number(pk), doctor_number(pk), service_date(pk),
service_number)

REBATE(service_number(pk), cover_type(pk), refund)

SERVICE(service_number(pk), service_name, service_description)

i am struggling in this query can any one help me

1)List the service-names and maximum refund payable for those services
which have been provided to every person who has visited a doctor.

this query asks for info about those services provided to every person
who has visited a doctor. For each such services, list the name of the
service and the maximum refund for that service (i.e. the same service
can have many different refunds, only list the highest refund). Maximum
refund therefore refers to the maximum for each such service, not a
maximum across all services.

2)what is the name and address of the doctor, or doctors, who have
provided services to the smallest non-zero number of individual
patients (regardless of the number of services they provided to
each patient)?



i dont the answers for these queries if anyone help me in this regard i
would be so thankful
--
Posted via http://dbforums.com
Jul 20 '05 #1
2 1308
Please include proper DDL with your posts, including constraints and
datatypes. Here are my solutions based on assumptions about your foreign
keys:

1)
SELECT S.service_name, SUM(R.refund) AS maximum_refund
FROM Rebate AS R
JOIN Service AS S
ON R.service_number = S.service_number
JOIN Provided_Service AS P
ON S.service_number = P.service_number
JOIN Doctor AS D
ON P.doctor_number = D.doctor_number
GROUP BY S.service_name
WHERE D.doctor_number = @doctor_number

2)
SELECT D.doctor_number, D.surname, D.initials, D.street_address, D.suburb,
COUNT(DISTINCT P.person_number) AS no_of_patients
FROM Doctor AS D
JOIN Provided_Service AS P
ON D.doctor_number = P.doctor_number
GROUP BY D.doctor_number, D.surname, D.initials, D.street_address, D.suburb
HAVING COUNT(DISTINCT P.person_number) <= ALL
(SELECT COUNT(DISTINCT person_number)
FROM Provided_Service
GROUP BY doctor_number)

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2

thanks for your reply

SELECT S.service_name, MAX(R.refund) AS maximum_refund

FROM Rebate AS R

JOIN Service AS S

ON R.service_number = S.service_number

JOIN Provided_Service AS P

ON S.service_number = P.service_number

JOIN Doctor AS D

ON P.doctor_number = D.doctor_number

GROUP BY S.service_name

WHERE D.doctor_number = @doctor_number

here i dont understand the last line can you explain it a bit

i think the query will bring up the maximum refund for those services
which doctor attended.

my question is maximum refund for those services for a person who
attends a doctor and

the value of refund which is dependend on the cover_type of a member
--
Posted via http://dbforums.com
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by James | last post: by
9 posts views Thread by netpurpose | last post: by
9 posts views Thread by Dom Boyce | last post: by
5 posts views Thread by Steve Patrick | last post: by
6 posts views Thread by Takeadoe | last post: by
47 posts views Thread by Jo | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.