简体   繁体   中英

get the column value for the two consecutive rows having the same other column values

Column 1: Date Column 2: Type

So if rows are sorted in descending order by Date , then find the Date during which user has the same consecutive Type "Clever" (See example below).

For exampe:

If my table contains the following data-

  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 Clever

So I want the Date 2013-05-27T21:45:46.127Z

I tried the following-

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;
        }
    }

But this is not giving me the Date for the consecutive rows that have the Type as "Clever" .

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();
}

After that, dateOfLastCleverPrecededByADuplicate is "2013-05-27T21:45:46.127Z" in your example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM