【发布时间】:2018-12-22 07:42:18
【问题描述】:
我有一个表格,其中有一个以日期格式命名的名为 sell_date 的列。
在本专栏中,我有以下日期,格式为“MM/dd/yyyy”。
我想要的是选择按周和月排序的行。就像我从微调器中选择一月和一年一样,它将显示该月-年格式的所有行。 而且每周我都不知道该怎么做:(
有这样的 SQLite 函数吗??
我已经尝试过 strftime('%m', sell_date ) 但没有用。
谢谢。
【问题讨论】:
标签: sqlite
我有一个表格,其中有一个以日期格式命名的名为 sell_date 的列。
在本专栏中,我有以下日期,格式为“MM/dd/yyyy”。
我想要的是选择按周和月排序的行。就像我从微调器中选择一月和一年一样,它将显示该月-年格式的所有行。 而且每周我都不知道该怎么做:(
有这样的 SQLite 函数吗??
我已经尝试过 strftime('%m', sell_date ) 但没有用。
谢谢。
【问题讨论】:
标签: sqlite
你可以使用
ORDER BY (substr(sell_date,7,4)||substr(sell_date,1,2)||substr(sell_date,4,2));
这需要严格遵守 MM/dd/yyyy 即 01,02..... 日和月部分(例如 2018 年 1 月 1 日不会按预期工作)。
但是,最好将日期存储在一种可识别的格式中,这种格式可以进行排序,另外还允许通过内置的日期和时间函数(例如 strftime )管理日期(这是因为日期不是经过调整的格式,strftime('%m', sell_date ) 不适合你)。
您可以使用 strftime('%m',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2)) 而不是 strftime('%m', sell_date )(同样,仅在严格遵守 MM/dd/yyyy 的情况下)
关于每周,您可以使用 %W 来获取一年中的星期,如果这就是您所说的每周的意思。同样,如果日期采用可识别的格式,那么strftime('%W',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2)) 将返回一年中的星期。
同时应用,你可以使用:-
ORDER BY
(substr(sell_date,7,4)||substr(sell_date,1,2)||substr(sell_date,4,2)),
strftime('%W',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2))
SQL As Understood By SQLite - Date And Time Functions.
考虑以下产生两个结果。第一个选择所有行(3 插入日期为 10/14/2018(第 41 周)、09/12/2018(第 37 周)和 11/13/2018(第 46 周)按预期顺序(按月然后按周) )。
为了说明,会生成显示一年中的月份和星期的列。在第二个查询中也使用了 week 列来选择与一周匹配的行(第 37 周又名 09/12/2018):-
DROP TABLE IF EXISTS table1;
CREATE TABLE IF NOT EXISTS table1 (sell_date TEXT);
INSERT INTO table1 VALUES ('10/14/2018'),('09/12/2018'),('11/13/2018');
SELECT *,
strftime('%m',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2)) AS month,
strftime('%W',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2)) AS week
FROM table1
ORDER BY
(substr(sell_date,7,4)||substr(sell_date,1,2)||substr(sell_date,4,2)),
strftime('%W',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2))
;
SELECT *,
strftime('%m',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2)) AS month,
strftime('%W',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2)) AS week
FROM table1
WHERE CAST(week AS INTEGER) = 37
ORDER BY
(substr(sell_date,7,4)||substr(sell_date,1,2)||substr(sell_date,4,2)),
strftime('%W',substr(sell_date,7,4)||'-'||substr(sell_date,1,2)||'-'||substr(sell_date,4,2))
第一个查询结果:-
第二个,使用 WHERE 子句(将值转换为 INTEGER)根据一年中的特定周选择行,结果是:-
现在,如果日期以可识别的格式 (YYYY-MM-DD) 存储,那么上面会更简单:-
DROP TABLE IF EXISTS table2;
CREATE TABLE IF NOT EXISTS table2 (sell_date TEXT);
INSERT INTO table2 VALUES ('2018-10-14'),('2018-09-12'),('2018-11-13');
SELECT *, strftime('%m',sell_date), strftime('%W',sell_date)
FROM table2
ORDER BY sell_date, strftime('%W',sell_date)
;
SELECT *, strftime('%m',sell_date), strftime('%W',sell_date)
FROM table2
WHERE CAST(strftime('%W',sell_date) AS INTEGER) = 37
ORDER BY sell_date, strftime('%W',sell_date)**strong text**
【讨论】: