获取具有相同其他列值的两个连续行的列值

时间:2013-05-29 12:06:55

标签: java android mysql sql

Column 1: Date Column 2: Type

因此,如果行按Date的降序排序,则找到用户具有相同连续类型"Clever"的日期(参见下面的示例)。

例如:

如果我的表包含以下数据 -

  Sr   Date                            Type

  1  2013-05-24T16:21:06.728Z       Alaska

  2  2013-05-27T20:44:32.412Z       Clever

  3  2013-05-27T20:45:33.301Z       Clever

  4  2013-05-27T21:45:46.127Z       Clever

  5  2013-05-27T21:46:27.825Z       Self

6 2013-05-28T15:18:48.430Z聪明

所以我想要日期2013-05-27T21:45:46.127Z

我尝试了以下内容 -

ArrayList<String> startTimeList = new ArrayList<String>();
cur = dbAdapter.rawQuery("select Date, Type from User ORDER BY Date DESC", null);

cur.moveToFirst();

if(cur.getCount() > 0)
{
    int i = 0;

    while(cur.isAfterLast() == false)
    {
        if(cur.getString(1).equals("Clever"))
        {
            startTimeList.add(cur.getString(0));
            cur.moveToNext();
        }
        else
        {
            cur.moveToNext();
        }
        if(startTimeList.size() == 2)
        {
            return;
        }
    }

但是对于类型为Date的连续行,这不会给我"Clever"

1 个答案:

答案 0 :(得分:2)

String dateOfLastCleverPrecededByADuplicate;

cur = dbAdapter.rawQuery("select Date, Type from User ORDER BY Date DESC", null);

cur.moveToFirst();
String prevType;
String prevDate;
while (!cur.isAfterLast())
{
    if (cur.getString(1) != null &&
        cur.getString(1).equals(prevType) &&
        cur.getString(1).equals("Clever"))
    {
        // found consecutives record with identical type 'Clever'
        dateOfLastCleverPrecededByADuplicate = prevDate;
        break;
    }

    prevDate = cur.getString(0);
    prevType = cur.getString(1);
    cur.moveToNext();
}

之后,dateOfLastCleverPrecededByADuplicate在您的示例中为“2013-05-27T21:45:46.127Z”。