2014-01-29 49 views
-1

我想與您覈對, 我想爲我的申請做一個總結。 這樣它會給我一個平均的價格基於月份。如何獲取指定月份的數據並將其除以天數

這意味着我有一組記錄,我已經按月過濾了這些記錄。 如果month = Jan,我希望得到所有Jan數據併除以當月的天數。 現在, 我只能拿出幾個月。 我想檢查, 如果我想這樣做「想獲得所有Jan數據併除以本月的天數」,我該怎麼辦? 任何人都可以向我建議嗎?

即時通訊新的android,並試圖學習的東西。

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.summary); 

     monthDate = (EditText)findViewById(R.id.month); 
     avgPrice = (TextView)findViewById(R.id.showavfPriceTV); 
     exFuel = (TextView)findViewById(R.id.showexFuelTV); 
     avgFC = (TextView)findViewById(R.id.showavgFCTV); 
     doneButton = (Button)findViewById(R.id.doneBTN); 
     exitButton = (Button)findViewById(R.id.exitBTN); 



      // First we need to make contact with the database we have created using 
      // the DbHelper class 
      AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this); 

      // Then we need to get a readable database 
      SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase(); 

      // We need a a guy to read the database query. Cursor interface will do 
      // it for us 
      // (String table, String[] columns, String selection, String[] 
      // selectionArgs, String groupBy, String having, String orderBy) 
      Cursor cursor = sqliteDatabase.query(
        AndroidOpenDbHelper.TABLE_NAME_LOG, null, null, null, null, 
        null, null); 
      // Above given query, read all the columns and fields of the table 

      startManagingCursor(cursor); 

      // Cursor object read all the fields. So we make sure to check it will 
      // not miss any by looping through a while loop 
      while (cursor.moveToNext()) { 
       // In one loop, cursor read one undergraduate all details 
       // Assume, we also need to see all the details of each and every 
       // undergraduate 
       // What we have to do is in each loop, read all the values, pass 
       // them to the POJO class 
       // and create a ArrayList of undergraduates 
       String id = cursor.getString(cursor 
         .getColumnIndex(AndroidOpenDbHelper.KEY_ROWID)); 

       final String date = cursor.getString(cursor 
         .getColumnIndex(AndroidOpenDbHelper.KEY_DATE)); 
       String price = cursor.getString(cursor 
         .getColumnIndex(AndroidOpenDbHelper.KEY_PRICE)); 
       String pump = cursor.getString(cursor 
         .getColumnIndex(AndroidOpenDbHelper.KEY_FUEL)); 
       String cost = cursor.getString(cursor 
         .getColumnIndex(AndroidOpenDbHelper.KEY_COST)); 
       String odm = cursor.getString(cursor 
         .getColumnIndex(AndroidOpenDbHelper.KEY_ODM)); 
       String preodm = cursor.getString(cursor 
         .getColumnIndex(AndroidOpenDbHelper.KEY_PREODM)); 
       String fcon = cursor.getString(cursor 
         .getColumnIndex(AndroidOpenDbHelper.KEY_CON)); 



      // If you don't close the database, you will get an error 
      sqliteDatabase.close(); 
       Log.i("FuelLog", "dataBundle " + date); 

     monthDate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // showDialog(DATE_DIALOG_ID); 
       DialogFragment newFragment = new DatePickerFragment(); 
       newFragment.show(getFragmentManager(), "datePicker"); 
      } 
     }); 


doneButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 




       if (monthDate.getText().toString().equals(date.subSequence(3,9).toString())) 

{ 

        Log.d("MONTHDATE","date : "+ monthDate.getText().toString()); 
        Log.d("BBUNDLEDATE","date : "+ (date.subSequence(3,9).toString())); 


        Intent intent=new Intent(getApplicationContext(),homeActivity.class); 
        startActivity(intent); 


       } 
       else 
       { 
        Intent intent=new Intent(getApplicationContext(),about.class); 
        startActivity(intent); 
        Log.d("ELSEMONTHDATE","date : "+ monthDate.getText().toString()); 
        Log.d("ELSEBUNDLEDATE","date : "+ (date.subSequence(3,9).toString())); 
       } 






      } 
     }); 
} 

    } 

回答

2

假如你有你的月度數據單月的總和,

Cursor cur = myDB.rawQuery 
    ("SELECT SUM(Price) FROM Prices WHERE Month = 1", null); 
if(cur.moveToFirst()) 
{ 
    return cur.getInt(0); 
} 

更妙的是,你可以有一個單一查詢來獲取每的每個月的月和總結價格給定年份:

Calendar cal = new GregorianCalendar(2014, Calendar.JANUARY, 1); // Specify an year and a month basing on your datum 
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 

String sql = "SELECT Month, SUM(Price) FROM Prices WHERE Year = ? GROUP BY Month ORDER BY Month"; 

通過把你的單月數據

只要記住這一點:(弗洛姆SQLite的參考網站)

總和(X)
總(X) 的總和()和總()聚合函數返回所有的總和組中的非空值。如果沒有非NULL輸入行,則sum()返回NULL,但total()返回0.0。 NULL對於沒有行的總和通常不是有用的結果,但是SQL標準要求它,並且大多數其他SQL數據庫引擎以這種方式實現sum(),所以SQLite以相同的方式執行它以便兼容。提供非標準的total()函數是一種便捷的方式,可以解決SQL語言中的這個設計問題。

The result of total() is always a floating point value. The result of sum() is an integer value if all non-NULL inputs are integers. If any input to sum() is neither an integer or a NULL then sum() returns a floating point value which might be an approximation to the true sum. 

Sum() will throw an "integer overflow" exception if all inputs are integers or NULL and an integer overflow occurs at any point during the computation. Total() never throws an integer overflow. 

還要注意

它總是把虛擬值(0.0),用於在沒有價格被指定

+0

我不明白個好主意, 你能進一步解釋? – Chloe

+0

我只是混淆瞭如何獲取數據。 例如, 我有一組記錄,2條記錄是1月,3條記錄是2月 如果我點擊摘要(feb), 我想將FEB月份的PRICE記錄加在一起,然後除以天數。 我該怎麼做? – Chloe

+0

如何添加PRICE? – Chloe

相關問題