Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: java computation issue

  1. #1
    Junior Member datathinker's Avatar
    Join Date
    Jul 2013
    Location
    Beijing, China
    Posts
    7
    My Mood
    Daring
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java computation issue

    Question background:

    One Bank Information System needs to figure out mobile average(MA)( On the chart, the mobile average is a trend line which smoothes the recurrences of the days and provides you with a quick overview of the period trend) amount of monthly loan from Loan Table defined as below,


    Time amount
    3/14/2011 $43,334.10
    3/15/2011 $92,304.10 ...
    3/16/2011 $45,983.80 ...
    3/17/2011 $36,973.10 ...
    3/18/2011 $24,987.87 ...
    ... ... ...
    It is easy for SQL to calculate monthly loan by grouping by. But I don't know how to make it out with sql as relative location and cross row computation are involved , Soppose we do it purely with java, following the codes:
    st=conn.prepareStatement("select sum(amount)amount, to_char(time,'MM')month from loan  where to_char(time,'yyyy')=to_char(sysdate,'yyyy') group by to_char(time,'MM') order by month",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    			ResultSet rs=st.executeQuery();
    			int size=rs.getFetchSize();
    			for(int currentPos=1;currentPos<=size;currentPos++){
    				rs.absolute(currentPos);
    				float preAmount=-1,thisAmount=-1,nextAmount=-1;
    				float avgAmount=-1;
    				String month=rs.getString("month");
    				thisAmount=rs.getFloat("amount");
    				if(currentPos==1){	
    					rs.next();
    					nextAmount=rs.getFloat("amount");
    					avgAmount=(thisAmount+nextAmount)/2;
    				}else if(currentPos==size){
    					rs.previous();
    					preAmount=rs.getFloat("amount");
    					avgAmount=(thisAmount+preAmount)/2;
    				}else{
    					rs.previous();
    					preAmount=rs.getFloat("amount");
    					rs.next();
    					rs.next();
    					nextAmount=rs.getFloat("amount");
    					avgAmount=(thisAmount+nextAmount+preAmount)/3;
    				}
    				System.out.println(month+"   "+avgAmount);	
    			}
    			rs.close();
    ....
    Except pure java coding, I think this kind of data process job could also be done by sql or esProc and maybe esProc will be easier. But I don't know too much about sql, hope people viewing my thread could give some tips.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: java computation issue

    Are you asking how to do this using SQL? This is a Java forum, not a SQL forum.

    I can move this thread to the "other languages" forum and hope somebody knows more about SQL if you want, but I'd suggest posting on a SQL forum instead. Make sure you link between crossposts though.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 0
    Last Post: July 12th, 2013, 02:52 AM
  2. Simple computation program with strings...
    By alias22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 6th, 2012, 01:11 PM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  5. High / low integer computation algorithm????
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 12:40 PM