-1

我想创建一个查询,返回本周特定日期的行数。例如,如果dayNumber = 1,它应该返回 BookSaveTime 是星期一的书籍数量。如果 等,它应该返回星期二的计数dayNumber = 2

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

目前,此查询返回自一周开始以来的书籍数量 - 但我对如何修改它以使其返回给定指定日期的计数感到困惑 - 例如,星期一或星期二。

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

书类:

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");
            }
        }
    }
}
    
4

1 回答 1

0

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

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

注意:您可以WHERE BookSaveTime BETWEEN '2010-01-01' AND '2010-01-31'在 T1 内添加以选择特定日期范围内的数据。DB-Fiddle中的演示代码。

于 2021-02-09T03:29:20.437 回答