【问题标题】:How to query data based on a date using Fiscal Year on Sqlite如何在 Sqlite 上使用会计年度根据日期查询数据
【发布时间】:2015-01-13 19:58:15
【问题描述】:

我正在为客户修复一个 android 应用程序,我需要查询一个 sqlite 数据库以显示他们需要的报告。

报告应仅显示当前财政年度的数据(从 4 月 1 日开始,到 3 月 31 日结束)。以前的一位开发人员做过这个查询:

... WHERE sales_date BETWEEN date('now','localtime','start of year','+3 months') "
                       +  "AND  date('now','localtime','start of year', '+1 year', '+3 months','-1 day') "

此查询目前不起作用,因为例如在 2015 年 1 月,它显示 2015 年 4 月至 2015 年 3 月的数据,而它应该显示 2014 年 4 月至 2015 年 3 月的数据。

我正在为 sqlite 寻找优化的解决方案,因为我找到了其他 SQL 的类似答案(Truncate date to fiscal yearUse TO_CHAR function to display the Fiscal Year

【问题讨论】:

    标签: sqlite android-sqlite


    【解决方案1】:

    在前三个月,您想要上一年。 所以在年初之前减去三个月:

    BETWEEN date('now', 'localtime', '-3 months', 'start of year', '+3 months') "
        AND date('now', 'localtime', '-3 months', 'start of year', '+1 year', '+3 months', '-1 day')
    

    【讨论】:

    • 非常感谢您的回答!我认为可能有一种更自动的方式来执行此操作,我必须在创建 where 子句的代码之外创建一个 if 并区分这两种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多