Ok, I think I'm still trying to understand the problem. But I think that when accessing the database you could save the totals for XTSM, XTS, XRX and XHAN.
Or just calculate COUNT, where is XTSM "?" (in prepared condition). I understand that perhaps this is a large part of the database, which is already quite large.
In any case, you can also do this in java, since in the last for loop, I == COUNT. By the way, your pseudocode must define XTS in XTSM, because if it does not travel across all XTS in the database, not all XTS are associated with your specific XTSM.
There are more ideas in your four forces (I always wanted to say this :)), you could add some ifs values ββto the counter, so you know if you loop XTS, which has a logical relationship with XTSM and again at the end of the loop I = COUNT.
A little more information would be great, as I think I don't understand the problem in detail. How do you retrieve data from a database? Do you have a class that stores this? Maybe you could make some methods to calculate this.
I think a simple class
public class Foo {int XTSM; int XTS; int XRX; int XHAN; }
and then storing all the lines in the list (or a vector or map, something iterable), counting the elements in the list that have XTSM = x and XTS = y Will this do more or less what you are trying to do?
Edit: maybe this code helps, I donβt know if this is what you are looking for ... allthought I think maybe some kind of PL (if you are in ORACLE), do it faster. Anyway, some jave code ...
public class Foo { int XTSM; int XTS; int XRX; int XHAN; LinkedList lk;//maybe not a good idea to have it here //but hey, it a 30seconds thought... public void getItAll() throws Exception{//mostly SQLException, but any you need Connection conn=null; //make the connection String query="SELECT XTSM, XTS, XRX, XHAN " + " FROM your_table" + " WHERE 1=1" + " AND 1=1";//add stuff you need to the query Statement stmt= conn.createStatement(); ResultSet rst = stmt.executeQuery(query); Foo f = null; while(rst.next()){ f = new Foo(); //yeah, you should have getters and setters f.XTSM = rst.getInt("XSTM"); f.XTS = rst.getInt("XST"); f.XRX = rst.getInt("XRX"); f.XHAN = rst.getInt("XHAN"); this.lk.add(f); //this gives you all the table in a list (especifically a linkedlist } } public void calculateItAll() throws Exception{//some exception, probably IO? Foo f = null; int count =0; for(int i=0; i< this.lk.size(); i++){//we iterate through all the table f = new Foo(); f = (Foo)this.lk.get(i); count = 0; for(int j=0; j<=200;j++){//XSTM if(f.XTSM == j){//I keep thinking that getters are good for(int k=0; k<=2;k++){//XST if(f.XTS == k){//do the getters, really for(int m=0;m<=3;m++){//XRX if(f.XRX==m){//add funny comment about getters for(int n=0;n<=7;n++){//XHAN if(f.XHAN==n){//man, so good that is almost finished count++; }//end of if }//end of XHAN }//end of if }//end of XRX }//end of if }//end ofXST }//end of if }//end of XSTM //here you write the count and all the Foo somewhere } }}