我正在尝试从表的一列计算相同的值并返回值和计数器,例如:值1重复3次,值2重复2次,...
到目前为止,我的程序可以在数组中存储所需列的所有值。我的问题是我不知道如何将每个值与其他值进行比较。我知道我需要一个计数器,也许还需要一个阵列。所以我陷入了If条件。
try{
Statement stmt = conn.createStatement();
//Statement stmt1 = conn.createStatement();
ResultSet rset = stmt.executeQuery( "SELECT N_REGIONKEY FROM NATION " );
List N_REGIONKEY = new ArrayList ();
while (rset.next()){
N_REGIONKEY.add(rset.getInt("N_REGIONKEY"));
if (rset.next ==N_REGIONKEY){
.
.
.
}
//System.out.println(N_REGIONKEY);
}
}
这是我在你的帮助下写的新版本,答案仍然不是它应该是什么..
尝试{
System.out.println(“计算NATION表中的行数”);
Statement stmt = conn.createStatement();
//语句stmt1 = conn.createStatement();
ResultSet rset = stmt.executeQuery(“SELECT N_REGIONKEY FROM NATION”);
ArrayList N_REGIONKEY = new ArrayList();
while(rset.next()){
N_REGIONKEY.add(rset.getInt("N_REGIONKEY"));
int occurrences = 0;
for (int i = 0; i<N_REGIONKEY.size();i++)
{
occurrence = Collections.frequency(N_REGIONKEY,N_REGIONKEY.get(i));
System.out.println(“+ N_REGIONKEY.get(i)”有“+出现次数+”次数;
}
答案 0 :(得分:0)
int occurrences = 0;
for (int i = 0; i<N_REGIONKEY.size();i++){
occurrences = Collections.frequency(REGIONKEY, REGIONKEY.get(i));
System.out.println("There is " + ocurrences "ocurrences for " + N_RegionKey.get(i));
}
答案 1 :(得分:0)
尝试按sql查询计数,而不是数组
select N_REGIONKEY, count(N_REGIONKEY) as 'COUNT'
FROM NATION
group by N_REGIONKEY
答案 2 :(得分:0)
感谢每一个人的帮助。最后,我写了这个程序没有分组(尽管我知道它慢得多)。这个与“分组依据”具有相同的结果。
try{
System.out.println(“计算NATION表中的simillar行”);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(“SELECT N_REGIONKEY FROM NATION”);
ArrayList N_REGIONKEY = new ArrayList();
int [] MyCounter = new int [5];
while(rset.next()){
MyCounter[rset.getInt("N_REGIONKEY")] ++;
}
for(int i = 0; i&lt; 5; i ++){
if (MyCounter[i] > 0) {
System.out.println("There is " + MyCounter[i] + " ocurrences for " + i);
}