Group by month in SQLite

前端 未结 6 694
礼貌的吻别
礼貌的吻别 2020-12-16 14:17

I have an SQLite database which contains transactions, each of them having a price and a transDate.

I want to retrieve the sum of the transactions

6条回答
  •  有刺的猬
    2020-12-16 14:36

    In Sqlite, if you are storing your date in unixepoch format, in seconds:

    select count(myDate) as totalCount, 
    strftime('%Y-%m', myDate, 'unixepoch', 'localtime') as yearMonth
    from myTable group by strftime('%Y-%m', myDate, 'unixepoch', 'localtime');
    

    If you are storing the date in unixepoch format, in milliseconds, divide by 1000:

    select count(myDate/1000) as totalCount, 
    strftime('%Y-%m, myDate/1000, 'unixepoch', 'localtime') as yearMonth
    from myTable group by strftime('%Y-%m, myDate/1000, 'unixepoch', 'localtime');
    

提交回复
热议问题