简体   繁体   English

Arraylist 包含和保留所有结果与 Sql 内部连接不同

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

Sql output : 312 Sql Script inner join: Sql 输出:312 Sql 脚本内连接:

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 Jdbc 脚本 1:将 GLaccount 存储在数组列表 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 Jdbc 脚本 2:将 GLaccount 存储在数组列表 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我得到的输出:保留所有和包含方法中的 873 与 sql 输出不匹配: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.让我们假设第一个查询COST_TYPE_SELECTION包含 10 个有效行,并且带有DIM_HYP_PCCODE_FLATTENLEFT JOIN从 DIM 返回 5 行。

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.这意味着与EPM_CONSOLIDATED_LEDGER_M的内部连接将针对 5 行完成,因此即使所有行在此连接中都匹配,查询的总行数也将是 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.使用相同的数据,第一个 jdbc 查询将返回所有 10 行,因为没有内部联接,无法确定第二个 jdbc 查询将返回多少行,但从您的结果中可以肯定地说它会远远超过 5行。

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.因此,要在 Java 中解决此问题,您需要包含内部联接中使用的所有列,然后在所有这些列的列表之间执行与内部联接相同的匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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