0

我正在尝试从表的一列中计算相同的值并返回值和计数器,例如:值 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);
 }
}

这是我在您的帮助下写的新版本,但答案仍然不是应该的..

try{

  System.out.println("counting rows in NATION table");

  Statement stmt = conn.createStatement();

//  Statement 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++)
{    
occurrences = Collections.frequency(N_REGIONKEY,N_REGIONKEY.get(i) );

System.out.println("There is " + occurrences + "occurrences for " + N_REGIONKEY.get(i));

}
4

3 回答 3

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));
}
于 2013-04-24T01:34:16.160 回答
0

尝试按 sql 查询计数,而不是按数组

select N_REGIONKEY, count(N_REGIONKEY) as 'COUNT' 
FROM NATION
group by N_REGIONKEY
于 2013-04-24T01:59:21.650 回答
0

感谢大家的帮助。最后,我编写了这个程序,没有分组(尽管我知道它要慢得多)。这与“分组依据”的结果相同。

  try{

System.out.println("统计 NATION 表中的相似行");

语句 stmt = conn.createStatement();

ResultSet rset = stmt.executeQuery("SELECT N_REGIONKEY FROM NATION");

ArrayList N_REGIONKEY = new ArrayList ();

int[] MyCounter = 新的 int[5];

而(rset.next()){

  MyCounter[rset.getInt("N_REGIONKEY")] ++;

}

对于 (int i = 0; i<5;i++){

    if (MyCounter[i] > 0) {

        System.out.println("There is " + MyCounter[i] + " ocurrences for " + i);
    }
于 2013-04-29T00:28:08.777 回答