help in sql query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hallosenthil

    #1

    help in sql query


    the relations are



    MEMBERSHIP(memb ership_number (pk), cover_type(pk),
    start_date(pk), end_date)

    PERSON(person_n umber(pk), surname, initials, street_address, suburb, ,
    membership_numb er)

    DOCTOR(doctor_n umber(pk), surname, initials, street_address, suburb)

    PROVIDED-SERVICE(person_ number(pk), doctor_number(p k), service_date(pk ),
    service_number)

    REBATE(service_ number(pk), cover_type(pk), refund)

    SERVICE(service _number(pk), service_name, service_descrip tion)





    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
  • David Portas

    #2
    Re: help in sql query

    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_numbe r = S.service_numbe r
    JOIN Provided_Servic e AS P
    ON S.service_numbe r = P.service_numbe r
    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_addres s, D.suburb,
    COUNT(DISTINCT P.person_number ) AS no_of_patients
    FROM Doctor AS D
    JOIN Provided_Servic e AS P
    ON D.doctor_number = P.doctor_number
    GROUP BY D.doctor_number , D.surname, D.initials, D.street_addres s, D.suburb
    HAVING COUNT(DISTINCT P.person_number ) <= ALL
    (SELECT COUNT(DISTINCT person_number)
    FROM Provided_Servic e
    GROUP BY doctor_number)

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --


    Comment

    • hallosenthil

      #3
      Re: help in sql query


      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_numbe r = S.service_numbe r

      JOIN Provided_Servic e AS P

      ON S.service_numbe r = P.service_numbe r

      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

      Comment

      Working...