希望我能比标题更好地解释这一点。
我有一个活动表,如下所示:
|ID| |LicenseNumber| |DateTime| |1 | |123 | |2017-11-17 11:19:04.420| |2 | |123 | |2017-11-26 10:16:52.790| |3 | |123 | |2018-02-06 11:13:21.480| |4 | |123 | |2018-02-19 10:12:32.493| |5 | |123 | |2018-05-16 09:33:05.440| |6 | |123 | |2019-01-02 10:05:25.193|
我需要的是每个许可证号的行数,基本上按12个月的间隔进行分组。但是,年份需要从上一个条目结束时开始。
例如,我需要统计2017年11月17日11:19:04.420起12个月的所有记录,然后我需要统计自(2017-11-17 11:19:04.420 + 12个月)再进行12个月,依此类推。
我已经考虑过使用递归CTE,LAG函数等,但是还不太清楚。我可能可以对CASE语句和静态值执行某些操作,但这需要每年更新报告代码。
向我指出正确方向的任何帮助将不胜感激!
答案 0 :(得分:0)
我认为以下使用CTE的代码可以为您提供帮助,但我不确定要实现的目标:
{
"_shard": "[tech_places][4]",
"_node": "u7QI_gjjRXy4-xdqnK2KMw",
"_index": "tech_places",
"_type": "entity",
"_id": "8403",
"_score": 0.0,
"_source": {
"Id": 8403,
"Name": "RETE-43424",
"Description": "SRF-10kv №28 VISO",
"ParentId": null,
"OrganizationId": 12,
"OrganizationPath": "12_27",
"LocationId": 27,
"Classification": "",
"Type": "A",
"Status": 0,
"MaintenanceObjectId": null,
"TreePath": "8403"
},
"_explanation": {
"value": 0.0,
"description": "sum of:",
"details": [
{
"value": 0.0,
"description": "match on required clause, product of:",
"details": [
{
"value": 0.0,
"description": "# clause",
"details": []
},
{
"value": 0.0,
"description": "sum of:",
"details": [
{
"value": 0.0,
"description": "weight(Description:28 in 35112) [], result of:",
"details": [
{
"value": 0.0,
"description": "score(doc=35112,freq=1.0), with freq of:",
"details": [
{
"value": 1.0,
"description": "termFreq=1.0",
"details": []
}
]
}
]
}
]
}
]
}
]
}
},
答案 1 :(得分:0)
嗯。您仅需要第一条记录后每隔12个月的记录数吗?
如果是这样:
select dateadd(year, yr - 1, min_datetime),
dateadd(year, yr, min_datetime),
count(t.id)
from (values (1), (2), (3)) v(yr) left join
(select t.*,
min(datetime) over () as min_datetime
from t
) t
on t.datetime >= dateadd(year, yr - 1, min_datetime) and
t.datetime < dateadd(year, yr, min_datetime)
group by dateadd(year, yr - 1, min_datetime),
dateadd(year, yr, min_datetime)
order by yr;
如果您愿意的话,可以很容易地将其扩展到更多年。