DBAsupport.com Forums - Powered by vBulletin
Results 1 to 10 of 10

Thread: Query question

  1. #1
    Join Date
    Jan 2002
    Posts
    28
    Hi everyone,

    I need suggestions or idea. please help.

    I have three tables.

    patient (patientid, firstname, lastname, dob,
    street, city, state, phone)

    insuranceinfo(patientid,insuranceid,groupname,grouno,insurername,insurerid)

    insurance(insuranceid,insurancename,street,city,state,zip,phone)
    insurance table is a reference table.
    Patientid, insuranceid are the primary key for the
    insuranceinfo table.

    If patient A has insurance, the insuranceinfo table
    would have an entry for
    A.

    If patient B doesn’t have insurance then in the
    insuranceinfo table would
    not have anything.


    My question is how can I get all the patient’s
    information from these tables
    in one query?
    It doesn’t matter if the patient does or does not
    have insurance.



    Thanks in advance!



  2. #2
    Join Date
    Dec 2001
    Location
    Brazil
    Posts
    282
    maybe...

    SELECT P.Patientid, P.firstname, P.lastname, P.dob, P.street, P.city,
    P.state, P.phone,i.insuranceid, i.groupname, i.groupno, i.insurname,
    i.insurerid
    FROM Patient P, insuranceinfo i
    WHERE p.patientid = i.patientid;


    ??


    F.


  3. #3
    Join Date
    Feb 2002
    Posts
    5
    hi ,
    i go through your problem .
    and as per u'r requirement i make one query hope it will helpfull for you .

    SELECT P.Patientid, P.firstname, P.lastname, P.dob, P.street, P.city,
    P.state, P.phone,i.insuranceid, i.groupname, i.groupno, i.insurname,
    i.insurerid
    FROM Patient P, insuranceinfo i
    WHERE ( p.patientid = i.patientid
    or p.patientid not in (select patientid from insuranceinfo));


    hope it will solve your problem .

    sanju


  4. #4
    Join Date
    Feb 2002
    Posts
    2
    Use an outer join so you include the insurance details where they have insurance and none where they don't

  5. #5
    Join Date
    Mar 2002
    Posts
    1
    try outer join....or inner join
    SELECT P.Patientid, P.firstname, P.lastname, P.dob, P.street, P.city,
    P.state, P.phone,i.insuranceid, i.groupname, i.groupno, i.insurname,
    i.insurerid
    FROM Patient P, insuranceinfo i
    WHERE p.patientid(+) = i.patientid;



  6. #6
    Join Date
    Jan 2002
    Posts
    28

    query question

    Thanks a lot to all of you. Because one table can only outer join with maximum one of the other table. The information I need actually required outer join two tables -- patient table with insuranceinfo table then insuranceinfo with insurance. It will never work. Since I am in the design phrase of the database development, all I am going to do is to add an entry in the insurance table for those people who don't have insurance. This will solve the problem. Thanks again.

  7. #7
    Join Date
    Nov 2000
    Location
    Baltimore, MD USA
    Posts
    1,339
    Whoa!

    Unless I am missing something, you don't have a problem, except that maybe your outer-joins are backwards.

    Please supply the statement that is giving you the error and we'll see what we can do.

    Adding an entry to a table when there should not be one should be an absolute desperation measure. I find it hard to believe it is necessary here.

    - Chris
    Christopher R. Long
    ChrisRLong@HotMail.Com
    But that's just my opinion. I could be wrong

  8. #8
    Join Date
    Jan 2002
    Posts
    28

    query question

    Hello Chris,

    The following are the queries that I wrote:

    Query1

    select patient.patientid,firstname from insuranceinfo,patient where patient.patientid=insuranceinfo.patientid(+);

    This query will work if I don't need any information from the insurance table such as insurancename, insuranceaddress. It is not what I want.

    Query2

    select patient.patientid,firstname,insurancename from insurance,insuranceinfo,patient where pat
    ient.patientid=insuranceinfo.patientid(+) and insurance.insuranceid=insuranceinfo.insuranceid;

    This query will always bring me back the patient who have insurance.


    Any suggestions? Thanks.



  9. #9
    Join Date
    Apr 2001
    Location
    Czechia
    Posts
    712
    Originally posted by tang
    The information I need actually required outer join two tables -- patient table with insuranceinfo table then insuranceinfo with insurance. It will never work.
    It will work. You can outer-join a table to already outer-joined table:
    Code:
    select patient.patientid, firstname, insurancename
    from insurance,insuranceinfo,patient
    where patient.patientid=insuranceinfo.patientid(+)
    and insurance.insuranceid(+)=insuranceinfo.insuranceid;
    This is the case that will never work, because insuranceinfo is outer-joined to two tables:
    Code:
    select patient.patientid, firstname, insurancename
    from insurance,insuranceinfo,patient
    where patient.patientid=insuranceinfo.patientid(+)
    and insurance.insuranceid=insuranceinfo.insuranceid(+);
    Ales



  10. #10
    Join Date
    Jan 2002
    Posts
    28
    It works!
    Thank you so much everyone. It is a great feeling knowing that someone out there who truly want to help the others.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width