我应该如何在Sqlite中执行此查询?

时间:2012-03-17 16:54:01

标签: android database sqlite

我有一个包含列的数据库:id,pdate,pvalue1,pvalue2。首先,我用光标进行查询:

Cursor c = ourDatabase.query(DATABASE_TABLE, new String[] { "_id","pdate","pvalue1","pvalue2"},
                    "pdate >= ? AND pdate <= ?",  new String[] { datefrom, dateto }, null, null, null);   

这给了我一些行,例如,如果pdate = 20120318,那么pvalue1 = 58,pvalue2 = 29。这些是字符串,所以我可以给pvalue2一个值“XX”。我想在给定的datefrom和dateto之间求和pvalue1,并用pdate将它们分组,其中pvalue2 = XX。我的问题是我不能把这个条件放到查询中(因为它的工作,比如“pvalue2 = XX”..),因为我也需要其他数据。

if (c.moveToFirst())
{
     do{
         if (c.getString(3).equals("XX")){
         Log.i("XX", c.getString(1) + " " + c.getString(2)) + " " + c.getString(3));

          }
         else {
         Log.i("NotXX",  c.getString(1) + " " + c.getString(2)) + " " + c.getString(3));

          }

     while (c.moveToNext());
     }
}

到目前为止没关系,所以我可以用pvalue2 = XX和NotXX来记录数据,得到这样的结果: (pdate,pvalue1,pvalue2)20120317,48,29; ------; 20120317,21,54; ------- 20120317,11,XX; ----- 20120318,79,71; ------- 20120318,21,XX

我想做什么?

首先:用pdate对sums(pvalue1)进行分组,如果pvalue2是XX或者不是XX则表明它,所以像这样的somethnig: 20120317,NotXX,69(自48 + 21 = 69)-------- 20120317,XX,11 -------- 20120318,NotXX,79 -------- 20120318,XX ,21

在此之后,我想每天从NotXX总和中减去XX总和。我想得到:    20120317,58(自69-11)------- 20120318,58(自79-21以来)

我该怎么做? 非常感谢你提前!

1 个答案:

答案 0 :(得分:1)

  

我的问题是我无法将此条件纳入查询

你可能错了。您可以添加类似的内容(语法可能包含错误)

“选择总和(从DATABASE_TABLE中选择pdate,其中pdata&gt; x和pdate&lt; y)作为总和”

到投影参数,您将该结果作为名为sum的列获得。唯一的问题是投影中没有?的支持(至少我没有尝试过,但我想它不会起作用)

如果那不是你想要的,那么很可能会采用不同的方式。 SQLite非常强大。

编辑:

这会是你想要的吗?它不是在SQL中完成的,但它会打印出你想要的每一天的总和。

Cursor c = ourDatabase.query(DATABASE_TABLE, new String[] { "_id","pdate","pvalue1","pvalue2"},
        "pdate >= ? AND pdate <= ?",  new String[] { datefrom, dateto }, null, null, "pdate");
boolean first = true;
if (c != null) {
    String currentDate = null;
    int sum = 0;
    while (c.moveToNext()) {
        String date = c.getString(1);
        int value1 = c.getInt(2);
        String value2 = c.getString(3);
        if (!date.equals(currentDate)) {
            if (!first) {
                Log.d("TAG", "The result for " + currentDate + " is: " + sum);
            } else {
                Log.d("TAG", "Date has changed, but we don't have data yet.");
            }
            first = false;
            currentDate = date;
            sum = 0;
        }
        if ("XX".equals(value2)) {
            Log.d("TAG", "new line: " + date + ", " + value1 + ", " + value2 + " -");
            sum -= value1;
        } else {
            Log.d("TAG", "new line: " + date + ", " + value1 + ", " + value2 + " +");
            sum += value1;
        }
    }
    if (!first) {
        Log.d("TAG", "The last result: " + currentDate + " is: " + sum);
    }
    c.close();
}

Edit2:当您希望数据库完成此操作时,这可能会有效。

Cursor c = ourDatabase.rawQuery(
        "SELECT pdate, sum(sum2) AS sum1 FROM " +
        "(" +
        "   SELECT pdate, pvalue1, pvalue2, -sum(pvalue1) AS sum2 " +
        "      FROM " + DATABASE_TABLE +
        "      WHERE pvalue2='XX' GROUP BY pdate" +
        "   UNION " +
        "   SELECT pdate, pvalue1, pvalue2, sum(pvalue1) AS sum2 " +
        "      FROM " + DATABASE_TABLE + 
        "      WHERE pvalue2!='XX' GROUP BY pdate" +
        ") " +
        "   WHERE pdate>=? AND pdate<=? " +
        "   GROUP BY pdate",
        new String[] { datefrom, dateto });
if (c != null) {
    while (c.moveToNext()) {
        String date = c.getString(0);
        int value1 = c.getInt(1);
        Log.d("TAG", "The result for " + date + " is: " + value1);
    }
    c.close();
}