计算本周特定日期的行数sqlite

Count rows of a particular day this week sqlite

我想创建一个查询,其中 return 是本周特定日期的行数。例如,如果 dayNumber = 1,它应该 return BookSaveTime 为星期一的书籍数量。如果 dayNumber = 2,它应该 return 星期二的计数,依此类推。

var week = DateTime.Today.AddDays(-(((int)DateTime.Today.DayOfWeek+6)%7));
return database.ExecuteScalar<int>("SELECT count(*) FROM Book WHERE BookSaveTime > ?;", week);

目前,此查询 return 是自本周开始以来的图书数量 - 但我对如何修改它感到困惑,以便它 return 是给定图书的数量指定的日期 - 例如,星期一或星期二。

public MyAppSQLiteDatabase()
{
    try
    {
        database = DependencyService.Get<ISQLiteService>().GetConnection(DbFileName);
        database.CreateTable<Book>();

        CurrentState = "Database created";

    }
    catch (SQLiteException ex)
    {
        CurrentState = ex.Message;
    }
}

public int GetDailyCount(int dayNumber){
    var day = DateTime.Today.AddDays(-(((int)DateTime.Today.DayOfWeek+6)%dayNumber));
    return database.ExecuteScalar<int>("SELECT count(*) FROM Book WHERE BookSaveTime = ?;", day );
}

图书class:

public class Book: INotifyPropertyChanged
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    private DateTime bookSaveTime;
    public DateTime BookSaveTime
    {
        get
        {
            return bookSaveTime;
        }
        set
        {
            if (bookSaveTime != value)
            {
                bookSaveTime= value;
                OnPropertyChanged("BookSaveTime");
            }
        }
    }
}
    

要查找特定的工作日信息,您的查询应如下所示 =>

SELECT * FROM
(SELECT *, cast (strftime('%w', BookSaveTime) as integer) AS WeekDay
FROM BOOK) T1
WHERE T1.WeekDay=1; --Weekday=1 is MONDAY

要查找特定日期的计数,请使用以下查询

SELECT WeekDay,COUNT(*) AS WeekDayCount FROM
(SELECT *,strftime('%w',BookSaveTime) AS WeekDay
FROM BOOK) T1
GROUP BY T1.WeekDay

注意: 您可以在 T1 内添加 WHERE BookSaveTime BETWEEN '2010-01-01' AND '2010-01-31' 到特定日期范围内的 select 数据。 DB-Fiddle.

中的演示代码