sql - 日期之间按周计数
问题描述
我试图按周显示计数,但我不确定如何找到 effdate 和 expdat 之间未显示的周。如何显示下面显示的星期和计数?谢谢。
解决方案
您可以使用递归查询来枚举周数,然后将其与表连接起来
with cte as (
select min(effweek) week, max(expweek) max_week from mytable
union all
select week + 1, max_week from cte where week < max_week
)
select c.week, count(t.id_num) cnt
from cte c
left join mytable t on c.week between t.effweek and t.expweek
group by c.week
order by c.week
DB Fiddle 上的(简化)演示:
周 | cnt ---: | --: 12 | 2 13 | 1 14 | 1
推荐阅读
- php - 声明一个存在于 foreach 循环中的数组?
- prometheus - 如何使用 PROMQL 查询特定指标的特定值
- javascript - PDF 查看可能性
- c++ - c++ STL中队列的数据插入和删除速度
- java - 使用apache POI在excel文件中写入单元格时出现问题
- javascript - 使用 puppeteer 在站点完全加载之前尝试获取屏幕截图和数据
- adaptive-cards - 自适应卡片从 if 条件更改文本颜色
- python - Django / Python:TypeError at /'NoneType'对象不可下标
- xamarin - NSData Sha512 哈希与 Xamarin
- django - Django all auth facebook login with Use Strict Mode for Redirect URIs