Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 统计给定月份满足条件的所有行_Sqlite - Fatal编程技术网

Sqlite 统计给定月份满足条件的所有行

Sqlite 统计给定月份满足条件的所有行,sqlite,Sqlite,我需要做一个SQLite查询,查找一些我无法完全理解的东西 我有一个数据库表,其中有一堆“问题”。每个“问题”都有一个createddate和resolutiondate id createddate resolutiondate ------------------------------------------- 1 2019-04-18 2019-08-18 2 2019-04-20 2019-04-21 3

我需要做一个SQLite查询,查找一些我无法完全理解的东西

我有一个数据库表,其中有一堆“问题”。每个“问题”都有一个createddate和resolutiondate

id        createddate     resolutiondate
-------------------------------------------
1          2019-04-18      2019-08-18
2          2019-04-20      2019-04-21
3          2019-05-08      2019-06-05
etc....
我需要做的是,计算在过去的12个月中,每个月有多少“问题”有一个创建日期。我想要一张这样的桌子:

Month         No. Of Issues Not Resolved But Existed That Month
---------------------------------------------------------------
2019-04       20
2019-05       17
2019-06       15
etc...

我很挣扎,因为我基本上需要多次检查每一行,对于每个月,它的创建日期都是,使用递归的
CTE
返回过去12个月的数据,并将
左连接到表中:

with months as (
  select strftime('%Y-%m', 'now', '-1 year') month
  union all 
  select strftime('%Y-%m', strftime('%Y-%m-%d', month || '-01', '+1 month') )
  from months 
  where month < strftime('%Y-%m', 'now', '-1 month')
)   
select m.month, 
  count(id) [No. Of Issues Not Resolved But Existed That Month] 
from months m left join tablename t
on strftime('%Y-%m', t.createddate) <= m.month and strftime('%Y-%m', t.resolutiondate) > m.month
group by m.month

请附上包含原始数据的样本表。使用我们看不到的数据拼凑查询是非常困难的。至少将日期的格式更改为类似YYYY-MM-DD的格式
with months as (
  select strftime('%Y-%m', 'now', '-1 year') month
  union all 
  select strftime('%Y-%m', strftime('%Y-%m-%d', month || '-01', '+1 month') )
  from months 
  where month < strftime('%Y-%m', 'now', '-1 month')
)   
select m.month, 
  count(id) [No. Of Issues Not Resolved But Existed That Month] 
from months m left join tablename t
on strftime('%Y-%m', t.createddate) <= m.month and strftime('%Y-%m', t.resolutiondate) > m.month
group by m.month
| month   | No. Of Issues Not Resolved But Existed That Month |
| ------- | ------------------------------------------------- |
| 2019-02 | 0                                                 |
| 2019-03 | 0                                                 |
| 2019-04 | 1                                                 |
| 2019-05 | 2                                                 |
| 2019-06 | 1                                                 |
| 2019-07 | 1                                                 |
| 2019-08 | 0                                                 |
| 2019-09 | 0                                                 |
| 2019-10 | 0                                                 |
| 2019-11 | 0                                                 |
| 2019-12 | 0                                                 |
| 2020-01 | 0                                                 |