1 Reply Latest reply: Feb 28, 2013 7:46 AM by Frank Kulash RSS

    duplicates in sql query out put when qury has join on multiple tables.

    956118
      I have the below query whcih is returning 2 duplicate records.

      Is there any way how to figure out from which table is causing the duplicate?

      There are so many inline view and join conditions in this query .

      in waht join conditions i need to look into mainly?

      or how to find the whcih table is causing the duplicate when we have multimple joins?

      this is really bothering me a lot .please help me with this.




      SELECT       'BCCALGLB'            model_cd,
                           fi_instrument_id,
                           fmr_cusip             instrument_id,
                           price,
                           '27-FEB-2013'          pricing_dt,
                           currency_cd 
                           FROM         (SELECT  II.fmr_cusip,
                                    IAI.fi_instrument_id,
                                    IP.price,
                                    IP.currency_cd
                            FROM    (SELECT  IdxHldg.vendor_instrument_id,
                                             IdxHldg.index_cd,
                                             IdxHldg.data_source_cd,
                                             GM.member_rank,
                                             RANK () OVER (PARTITION BY  IdxHldg.vendor_instrument_id
                                                           ORDER BY      GM.member_rank  ASC)  priority
                                     FROM    (SELECT  IBH.vendor_instrument_id,
                                                      IBH.index_cd,
                                                      IBH.data_source_cd
                                              FROM    fi_idx_benchmark_holdings  IBH
                                              WHERE   IBH.pricing_dt = '27-FEB-2013'
      
                                              UNION
      
                                              SELECT  IFH.vendor_instrument_id,
                                                      IFH.index_cd,
                                                      IFH.data_source_cd
                                              FROM    fi_idx_forward_holdings  IFH
                                              WHERE   IFH.pricing_dt = '27-FEB-2013'
                                             )  IdxHldg,
                                             fi_group_member  GM
                                     
                                     WHERE   GM.group_cd      = 'BCGLOBALIDX'
                                     AND     GM.purpose_cd    = 'GLOBALIDX'
                                     AND     IdxHldg.index_cd = GM.character_val
                                    )  BCIdxHldg,
                                    fi_idx_instrument  II,
                                    fi_idx_price  IP,
                                    instrument_alternate_id  IAI,
                                    instrument  I
                            WHERE   BCIdxHldg.priority                  =      1
                            AND     BCIdxHldg.data_source_cd            =      II.data_source_cd
                            AND     BCIdxHldg.vendor_instrument_id      =      II.vendor_instrument_id
                            AND     II.data_source_cd                   =      IP.data_source_cd
                            AND     II.vendor_instrument_id             =      IP.vendor_instrument_id
                            AND     IP.currency_cd                      =      I.currency_cd
                            AND     IP.pricing_dt                       =      '27-FEB-2013'
                            AND     II.fmr_cusip                        =      IAI.alternate_id
                            AND     IAI.alternate_id_type_code          =      'FMR_CUSIP'
                            AND     IAI.fi_instrument_id                =      I.fi_instrument_id
                            AND     NVL (I.instrument_domain_cd, 'XXX') NOT IN ('MBS', 'MGEN')
                            AND     NVL (I.instrument_type_cd, 'XXX')   !=     'CMBS'
      
                            AND     ((I.currency_cd                     NOT IN ('CAD', 'USD'))
                                     OR
                                     (EXISTS (SELECT  1
                                              FROM    adm_calendar_date  ACD,
                                                      ctry_curr_link  CCL
                                              WHERE   TO_DATE (ACD.calendar_yyyymmdd, 'YYYYMMDD') = '27-FEB-2013'
                                              AND     ACD.calendar_origin_cd                      = 'EXCHANGE'
                                              AND     ACD.calendar_type_cd                        = 'BUSINESS'
                                              AND     ACD.geography_cd                            = CCL.ctry_cd
                                              AND     CCL.link_typ                                = 'ISS'
                                              AND     CCL.curr_cd                                 = I.currency_cd
                                              AND     ACD.business_day_ind                        = 'Y'))))
        • 1. Re: duplicates in sql query out put when qury has join on multiple tables.
          Frank Kulash
          Hi,
          953115 wrote:
          I have the below query whcih is returning 2 duplicate records.

          Is there any way how to figure out from which table is causing the duplicate?

          There are so many inline view and join conditions in this query .

          in waht join conditions i need to look into mainly?

          or how to find the whcih table is causing the duplicate when we have multimple joins?
          What has changed since the last time the query ran correctly? (It must have run correctly at some point. You didn't just write a query that big all at once without testing each individual part, did you?)

          Find a small set of sample data that causes the problem. Create and work with tables that have only that small set of data.

          Take baby steps. Start with the most deeply nested sub-query. and simplify it so that it uses only 1 table: comment out all references to other tables. Does it produce exactly what you're expecting, especially the number of rows?
          If not, post just that one sub-query, your sample data, and the output you need (but are not getting) from that sub-query.
          If it does work, then un-comment the other tables, 1 at a time. Test after each one. If the revised sub-query starts acting strangely, then you have a good clue about what's causing the problem, but if you can't figure it out, post that much of the query, your sample data (CREATE TABLE and INSERT statements) and the expected results from the code you posted.
          If then entire sub-query works, then add the next super-query, and repeat the testing process.

          What is the data type of ibh.pricing_dt?
          If its a DATE, then don't compare it to a VARCHAR2, as you're doing in this line:
          IBH.pricing_dt = '27-FEB-2013'
          Use TO_DATE to convert a string, such as '27-FEB-2013', to a DATE.
          If it's not a DATE (or TIMESTAMP), then you have an even more serious problem.