Uploaded image for project: 'Teiid'
  1. Teiid
  2. TEIID-4619

left join returns wrong results

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Critical Critical
    • None
    • 9.0.4, 9.0.5
    • None
    • None

      I have the following situation.

      I have a temporary table #tmp_admissions that contains 8047 rows.

      In this first query there are 66290 results. However if I only look at the lines for infectionid 880 then there are only 16 lines.

      select l.infectionid, l.id as linkid, lc.linkcultureid, lc.responsibleculture, lc.culturealternative, cl.sampleinsertts, cl.specimennumber, 
      				cl.culturenumber, cl.culturename, cl.quotation, ls.material, ls.sampletime,
      				abr.culturenumber as abgram_culturenumber,abr.antibiogrampart, abr.resisttype, 
      				lc.antibiogramculturenr,lc.antibiogramspecimennr,lc.antibiogramsampleinsertts
      			from #tmp_admissions adm
      			join cos2_links l on l.admissionid = cast(adm.patientid as string)
      			join cos2_link_culture lc on lc.linkid = l.id
      			left join cos2_lab_culture cl on cl.culturenumber = lc.culturenr and cl.specimennumber = lc.culturespecimennr and cl.sampleinsertts = lc.culturesampleinsertts
      			left join cos2_lab_sample ls on ls.inserttime = cl.sampleinsertts and ls.specimennumber = cl.specimennumber
      			left join cos2_antibiogramresistences abr on abr.specimennumber = cl.specimennumber and abr.culturenumber = cl.culturenumber and abr.sampleinsertts = cl.sampleinsertts
      

      This query does almost the same but returns 30 rows (and is correct).

      select  l.infectionid, l.id as linkid, lc.linkcultureid, lc.responsibleculture, lc.culturealternative, cl.sampleinsertts, cl.specimennumber, 
      				cl.culturenumber, cl.culturename, cl.quotation, ls.material, ls.sampletime,
      				abr.culturenumber as abgram_culturenumber,abr.antibiogrampart, abr.resisttype, 
      				lc.antibiogramculturenr,lc.antibiogramspecimennr,lc.antibiogramsampleinsertts
      from cos2_links l
      join cos2_link_culture lc on lc.linkid = l.id
      left join cos2_lab_culture cl on cl.culturenumber = lc.culturenr and cl.specimennumber = lc.culturespecimennr and cl.sampleinsertts = lc.culturesampleinsertts
      left join cos2_lab_sample ls on ls.inserttime = cl.sampleinsertts and ls.specimennumber = cl.specimennumber
      left join cos2_antibiogramresistences abr on abr.specimennumber = cl.specimennumber and abr.culturenumber = cl.culturenumber and abr.sampleinsertts = cl.sampleinsertts
      where l.infectionid = 880
      

      cos2_link_culture contains 2 rows for this infectionid. The left join statements should result in 15 rows for both rows. However the left join results in the first query for the first row are null and to my understanding ignored. I'll attach the query plans for both queries.

      I should note that there is a one to many relation between infection and admission so therefore infectionid is for the same admission.

      Strangely enough if you enclode the first query in a group by query and count the rows it does indeed return 2 times 15 for the specific groups (see enclosed_queryplan.txt).

        1. wrong_result.txt
          102 kB
        2. teiid_reduced_case.txt
          154 kB
        3. query2_plan.txt
          2 kB
        4. query1_plan.txt
          8 kB
        5. query1_enclosed_plan.txt
          8 kB
        6. enclosed_queryplan.txt
          122 kB
        7. correct_result.txt
          68 kB

              rhn-engineering-shawkins Steven Hawkins
              gadeynebram Bram Gadeyne (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: