简体   繁体   中英

Arraylist contain, & retain all not giving the same result as Sql inner join

Sql output : 312 Sql Script inner join:

SELECT count(*) FROM COST_TYPE_SELECTION CST
LEFT JOIN GBL_HYPACCT_RSLV_TBL_NEW_CA GBL
ON GBL.TREE_NODE = CST.GL_TREE_NODE
AND GBL.TREE_LEVEL = CST.GL_TREE_LEVEL
LEFT JOIN DIM_HYP_PCCODE_FLATTEN DIM
ON CST.PC_TREE_LEVEL = DIM.TREE_LEVEL
AND CST.PC_TREE_NODE = DIM.TREE_LEVEL_NODE
AND DIM.LEGAL_ENTITY='007'
AND DIM.PC_CODE='0200'
and CST.COST_TYPE='Direct_Expenses'
INNER JOIN EPM_CONSOLIDATED_LEDGER_M EPM
ON EPM.LEGAL_ENTITY = DIM.LEGAL_ENTITY
AND EPM.gl_profit_centre_cd = DIM.PC_CODE
AND EPM.GL_ACCOUNT_NUMBER = GBL.TREE_LEAF
WHERE  EPM.sourcedataloccd='SG'

Jdbc Script 1 : storing the GLaccount in arraylist Gllishive

SELECT GBL.TREE_LEAF FROM COST_TYPE_SELECTION CST
LEFT JOIN GBL_HYPACCT_RSLV_TBL_NEW_CA GBL
ON GBL.TREE_NODE = CST.GL_TREE_NODE
AND GBL.TREE_LEVEL = CST.GL_TREE_LEVEL
LEFT JOIN DIM_HYP_PCCODE_FLATTEN DIM
ON CST.PC_TREE_LEVEL = DIM.TREE_LEVEL
AND CST.PC_TREE_NODE = DIM.TREE_LEVEL_NODE
AND DIM.LEGAL_ENTITY='007'
AND DIM.PC_CODE='0200'

Jdbc Script 2 : storing the GLaccount in arraylist GllistMaria

select gl_account from EPM_CONSOLIDATED_LEDGER_M EPM
ON EPM.LEGAL_ENTITY ='007' AND EPM.gl_profit_centre_cd = '7482'
WHERE  EPM.sourcedataloccd='SG'

Then comparing the using below code

System.out.println("size of hive: " + Gllishive.size());
System.out.println("size of maria join: " + GllistMaria.size());
List<String> joins = new ArrayList<String>(GllistMaria);
joins.retainAll(Gllishive);
System.out.println("after retailall: " + joins.size());

ArrayList<String> MatchingGL = new ArrayList<String>();
for(int i=0;i<GllistMaria.size();i++){
if(Gllishive.contains(GllistMaria.get(i))){
MatchingGL.add(GllistMaria.get(i));
}
}       
System.out.println("after contains using for loop :"+MatchingGL);

Output I'm getting : 873 in both retain all and contains method was not matching with sql output : 312

Complete Code below

public void Script1(String ss)   {
    try{
      String consolm = "SELECT * FROM COST_TYPE_SELECTION CST\n" +
    "LEFT JOIN GBL_HYPACCT_RSLV_TBL_NEW_CA GBL ON GBL.TREE_NODE = CST.GL_TREE_NODE AND GBL.TREE_LEVEL = CST.GL_TREE_LEVEL\n" +
    "LEFT JOIN DIM_HYP_PCCODE_FLATTEN DIM ON CST.PC_TREE_LEVEL = DIM.TREE_LEVEL AND CST.PC_TREE_NODE = DIM.TREE_LEVEL_NODE\n" +
    "AND DIM.LEGAL_ENTITY='007'\n" +
    "AND DIM.PC_CODE=?" +
    "and CST.COST_TYPE='Direct_Expenses'\n";


        PreparedStatement sta2 = con.prepareStatement(consolm);
        sta2.setString(1, ss);
        ResultSet result3 = sta2.executeQuery();

        HashMap<String, List<String>> map = new HashMap<>();
        HashMap<String, List<String>> map5 = new HashMap<>();
        HashMap<String, List<String>> map6 = new HashMap<>();
        List<String> glAcs = new ArrayList<>();
        List<String> glAcs2 = new ArrayList<>();
        List<String> glAcs3 = new ArrayList<>();
        List<String> glAcs4 = new ArrayList<>();

        while(result3.next()){
            String de = result3.getString("gbl.descr");
            if(map.get(de)!=null){
              glAcs = map.get(de);
              glAcs.add(result3.getString("gbl.tree_leaf"));
              map.put(de,glAcs);
                data.add(result3.getString("gbl.tree_leaf"));
            }else{
                List<String> glAcs1 = new ArrayList<>();
                glAcs1.add(result3.getString("gbl.tree_leaf"));
                map.put(de,glAcs1);
                data.add(result3.getString("gbl.tree_leaf"));
            }


            String pe = result3.getString("gbl.descr");
            if(map5.get(pe)!=null){
                glAcs3 = map5.get(pe);
                glAcs3.add(result3.getString("cst.legal_entity"));
                map5.put(pe,glAcs3);
                data22.add(result3.getString("cst.legal_entity"));
            }
            else{ List<String> glAcs13 = new ArrayList<>();
                glAcs13.add(result3.getString("cst.legal_entity"));
                map5.put(pe,glAcs13);
                data22.add(result3.getString("cst.legal_entity"));
            }

            String he = result3.getString("gbl.descr");
            if(map6.get(he)!=null){
                glAcs4 = map6.get(he);
                glAcs4.add(result3.getString("dim.pc_code"));
                map6.put(he,glAcs4);
                data33.add(result3.getString("dim.pc_code"));
            }
            else{
                List<String> glAcs14 = new ArrayList<>();
                glAcs14.add(result3.getString("dim.pc_code"));
                map6.put(he,glAcs14);
                data33.add(result3.getString("dim.pc_code"));
            }
        }

        Set<String> hs = map.keySet();
        Iterator itr = hs.iterator();

        while(itr.hasNext()){
            String key = itr.next().toString();
           Glac.addAll(map.get(key));
                          }
        System.out.println(" Final count of hive was :"+Glac.size());
        System.out.println(" Final count of hive was :"+data.size());

        Set<String> hs22 = map5.keySet();
        Iterator itr22 = hs22.iterator();

        while(itr22.hasNext()){
            String key = itr22.next().toString();
            legen.addAll(map5.get(key));
        }
        System.out.println(" Final count of hive was :"+legen.size());
        System.out.println(" Final count of hive was :"+data22.size());

        Set<String> hs33 = map6.keySet();
        Iterator itr33 = hs33.iterator();

        while(itr33.hasNext()){
            String key = itr33.next().toString();
            pc.addAll(map6.get(key));
        }
        System.out.println(" Final count of hive was :"+pc.size());
        System.out.println(" Final count of hive was :"+data33.size());


        con.close();

    }catch(ClassNotFoundException |IOException e){

    }catch (SQLException e){
        e.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
        System.out.println(e.toString());
    }

}

public void script2(String ss)   {
    try{

        String consolm = "select * from EPM_CONSOLIDATED_LEDGER_M EPM\n" +
                "WHERE  EPM.sourcedataloccd='SG'AND EPM.primarysourcesyscd NOT IN('EPMCA','EPMTX','EPMOS')\n" +
                "AND EPM.businessdt='20181130' AND EPM.rundt='20181204' AND EPM.GL_BU_CD IN ('DBU','ACU')\n" +
                "AND EPM.LEGAL_ENTITY = '007'\n" +
                "AND EPM.gl_profit_centre_cd = ?";


        PreparedStatement sta2 = con.prepareStatement(consolm);
        sta2.setString(1, ss);
        ResultSet result3 = sta2.executeQuery();

        HashMap<String, List<String>> map = new HashMap<>();
        HashMap<String, List<String>> map3 = new HashMap<>();
        HashMap<String, List<String>> map4 = new HashMap<>();
        List<String> glAcs2 = new ArrayList<>();
        List<String> glAcs3 = new ArrayList<>();
        List<String> glAcs4 = new ArrayList<>();
                    while(result3.next())
                    {
            String de = result3.getString("epm.product_cd_hyperion");
            if(map.get(de)!=null){
                glAcs2 = map.get(de);
                glAcs2.add(result3.getString("epm.gl_account_number"));
                map.put(de,glAcs2);
                data2.add(result3.getString("epm.gl_account_number"));
            }else{
                List<String> glAcs12 = new ArrayList<>();
                glAcs12.add(result3.getString("epm.gl_account_number"));
                map.put(de,glAcs12);
                data2.add(result3.getString("epm.gl_account_number"));
            }


            String pe = result3.getString("epm.product_cd_hyperion");
                    if(map3.get(pe)!=null){
            glAcs3 = map3.get(pe);
            glAcs3.add(result3.getString("epm.legal_entity"));
            map3.put(pe,glAcs3);
            data3.add(result3.getString("epm.legal_entity"));
            }
            else{ List<String> glAcs13 = new ArrayList<>();
                glAcs13.add(result3.getString("epm.legal_entity"));
                map3.put(pe,glAcs13);
                data3.add(result3.getString("epm.legal_entity"));
            }

            String he = result3.getString("epm.product_cd_hyperion");
                        if(map4.get(he)!=null){
                            glAcs4 = map4.get(he);
                            glAcs4.add(result3.getString("epm.gl_profit_centre_cd"));
                            map4.put(he,glAcs4);
                            data4.add(result3.getString("epm.gl_profit_centre_cd"));
                        }
                        else{
                            List<String> glAcs14 = new ArrayList<>();
                            glAcs14.add(result3.getString("epm.gl_profit_centre_cd"));
                            map4.put(he,glAcs14);
                            data4.add(result3.getString("epm.gl_profit_centre_cd"));
                        }


        }

        Set<String> hs = map.keySet();
        Iterator itr = hs.iterator();

        while(itr.hasNext()){
            String key = itr.next().toString();

            Glac2.addAll(map.get(key));

        }
        System.out.println(" Final count of hive2 was :"+Glac2.size());
        System.out.println(" Final count of hive2 was :"+data2.size());

        Set<String> hs2 = map3.keySet();
        Iterator itr2 = hs2.iterator();

        while(itr2.hasNext()){
            String key = itr2.next().toString();

            legen2.addAll(map3.get(key));

        }
        System.out.println(" Final count of hive3 was :"+legen2.size());
        System.out.println(" Final count of hive3 was :"+data3.size());

        Set<String> hs3 = map4.keySet();
        Iterator itr3 = hs3.iterator();
        while(itr3.hasNext()){
            String key = itr3.next().toString();

            pc2.addAll(map4.get(key));

        }
        System.out.println(" Final count of hive4 was :"+pc2.size());
        System.out.println(" Final count of hive4 was :"+data4.size());

        con.close();

    }catch(ClassNotFoundException |IOException e){

    }catch (SQLException e){
        e.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
        System.out.println(e.toString());
    }

}

@Test
 public void Innerjoin() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {

    Script1("0200");
    script2("0200");
    ArrayList<String> MatchingGL = new ArrayList<String>();
    for (int i = 0; i < Glac2.size(); i++) {

        if (Glac.contains(Glac2.get(i))) {
            MatchingGL.add(Glac2.get(i));
        }
    }
    System.out.println("after contains using for loop :" + MatchingGL.size());

    ArrayList<String> MatchingGL2 = new ArrayList<String>();
    for (int i = 0; i < Glac2.size(); i++) {

        if (legen.contains(legen2.get(i))&&pc.contains(pc2.get(i))
                &&Glac.contains(Glac2.get(i))) {
            MatchingGL2.add(Glac2.get(i));
        }
    }
    System.out.println("after contains using for loop :" + MatchingGL2.size());

}

Lets assume for the 1st query that COST_TYPE_SELECTION contains 10 valid rows and that the LEFT JOIN with DIM_HYP_PCCODE_FLATTEN returns 5 rows from DIM.

This means the inner join with EPM_CONSOLIDATED_LEDGER_M will be done against 5 rows so if even if all rows are matched in this join the total number of rows for the query will be 5.

With the same data the first jdbc query will return all 10 rows since there is no inner join, how many rows the second jdbc query will return can not be determined but it feels safe to say from your results that it would be much more than 5 rows.

So to solve this in java you need to include all columns used in the inner join and then perform the same match as in the inner join between the lists for all those columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM