【问题标题】:How to use Group in Sqlite database w.r.t. DateColoumn?如何在 Sqlite 数据库中使用 Group w.i.t.h.日期栏?
【发布时间】:2023-03-09 23:13:02
【问题描述】:

我的 SQLite 表中有以下列: ActivityDate 的DataType 是Sqlite 提供的date

ActivityDate
2016-01-28 07:38:41 +0000
2016-01-28 03:26:56 +0000
2016-01-29 02:22:40 +0000
2016-01-26 01:13:39 +0000

我想获得唯一的日期(仅 w.r.t. 日期),我不想在其中考虑时间。 所以我想要的结果是:

ActivityDate
2016-01-28 07:38:41 +0000
2016-01-29 02:22:40 +0000
2016-01-26 01:13:39 +0000

那么,有没有办法使用 SQLite Query 来实现这一点?

【问题讨论】:

    标签: sqlite date time group-by


    【解决方案1】:

    这不是supported date formats 之一。

    为了能够使用内置的日期功能,你必须先剪掉时区:

    SELECT date(substr(ActivityDate, 1, 19))
    FROM transaction
    GROUP BY date(substr(ActivityDate, 1, 19));
    

    【讨论】:

      【解决方案2】:

      如果我理解正确,您只想按年-月-日期分组

      在这种情况下,您可能想在这里阅读: https://www.sqlite.org/lang_datefunc.html 尤其是 strftime() 函数

      这是另一个类似的问题 Group by month in SQLite

      【讨论】:

      • 我试过 select ActivityDate, strftime("%d-%m-%Y", ActivityDate) as 'date-month-year' from transaction group by strftime("%d—% m-%Y”, ActivityDate) 不工作
      • 我认为问题出在你给年月日的顺序上,你能像这样试试 strftime(%Y-%m-%d)
      • 最后我能想到的尝试是使用 date(ActivityDate) 而不是 strftime
      • 对不起,但没有帮助
      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2012-11-03
      • 2021-04-04
      • 1970-01-01
      • 2014-04-11
      相关资源
      最近更新 更多