假設我說我的財政年度開始日期是 2000-02-01。那么財政年度應該是
2019-03-01 to 2020-02-29
2020-03-01 to 2021-02-28
2021-03-01 to 2022-02-28
.
.
.
我想使用 SQLite 顯示如下所示的財政年度列。
| 產品 | 創建日期 | 財政年度 |
|---|---|---|
| 蘋果 | 2019-05-28 | 2019-2020財年 |
| 蘋果 | 2020-01-15 | 2019-2020財年 |
| 香蕉 | 2020-04-22 | 2020-2021 財年 |
| 芒果 | 2021-10-15 | 2021-2022 財年 |
uj5u.com熱心網友回復:
我懷疑維護一個單獨的表格,其中包含自定義財政年度范圍以及您要為每個范圍顯示的文本標簽:
financial_years:
start_date | end_date | label
2019-03-01 | 2020-02-29 | FY 2019-2020
2020-03-01 | 2021-02-28 | FY 2020-2021
2021-03-01 | 2022-02-28 | FY 2021-2022
現在我們需要做的就是將您的產品表加入上表:
SELECT p.Product, p.created_date, fy.label
FROM products p
INNER JOIN financial_years fy
ON p.created_date BETWEEN fy.start_date AND fy.end_date;
uj5u.com熱心網友回復:
不需要單獨的表。
所有你需要的是功能strftime():
SELECT *,
'FY ' || strftime('%Y', createdDate, '-2 month') || '-' ||
strftime('%Y', createdDate, '-2 month', ' 1 year') FinancialYear
FROM products;
請參閱演示。
uj5u.com熱心網友回復:
一個解決方案createdDate REAL和CASE WHEN:
select Product,
date(createdDate) as createdDate,
case when (abs(strftime('%m', createdDate)) < 3)
then 'FY ' || cast(strftime('%Y', createdDate) - 1 as text) || '-' || strftime('%Y', createdDate)
else 'FY ' || strftime('%Y', createdDate) || '-' || cast(strftime('%Y', createdDate) 1 as text)
end AS FinancialYear
from `products`;
在此處查看演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346535.html
標籤:sqlite
上一篇:在ASP.NETCore5.0WebAPI中實作DelegatingHandler?
下一篇:未創建sqflite表
