计算当前条纹 - sqlite

时间:2015-12-26 12:51:17

标签: java android sqlite

我带有会话的hava表

sessions
-----------------
_id | date 
-----------------
1   | 2015-12-25
2   | 2015-12-24
3   | 2015-12-23
4   | 2015-12-22
5   | 2015-12-22
6   | 2015-12-09
7   | 2015-11-04
8   | 2015-05-11

现在如何计算当前的连续条纹, 在上表中,当前条纹是22 dec-25 dec - > 4天

我可以使用不同的命令删除重复的日期

SELECT DISTINCT(date) FROM sessions ORDER BY date DESC;


  date 
  -----------------
  2015-12-25
  2015-12-24
  2015-12-23
  2015-12-22
  2015-12-09
  2015-11-04
  2015-05-11

1 个答案:

答案 0 :(得分:1)

SELECT (SELECT MAX(JULIANDAY(date)) FROM sessions) -
    COALESCE(
        (
            SELECT MAX(JULIANDAY(t.date))
            FROM
            (
                SELECT DISTINCT s1.date AS date, s1.date -
                    (SELECT MAX(s2.date)
                     FROM sessions s2
                     WHERE s2.date < s1.date) AS diff
                FROM sessions s1
            ) t
            WHERE t.diff > 1
        ),
        (SELECT MIN(JULIANDAY(date)) FROM sessions)
    )