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

时间:2019-03-18 11:03:13

标签: java arrays arraylist

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脚本1:将GLaccount存储在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脚本2:将GLaccount存储在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'

然后比较下面的使用代码

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);

我得到的输出:873中既保留了所有内容又包含了与SQL输出不匹配的方法:312

在下面填写代码

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());

}

1 个答案:

答案 0 :(得分:0)

让我们为第一个查询假设COST_TYPE_SELECTION包含10个有效行,而LEFT JOINDIM_HYP_PCCODE_FLATTEN一起从DIM返回5行。

这意味着使用EPM_CONSOLIDATED_LEDGER_M进行的内部联接将针对5行进行,因此即使该联接中的所有行均匹配,查询的总行数也将为5。

对于相同的数据,第一个jdbc查询将返回所有10行,因为没有内部联接,因此无法确定第二个jdbc查询将返回多少行,但是从您的结果中可以肯定地说这将是很多多于5行。

因此,要在Java中解决此问题,您需要包括内部联接中使用的所有列,然后执行与所有这些列的列表之间的内部联接中相同的匹配。