我获得了项目数据的视图,并被要求总结在给定月份中有多少项目是开放的。
PROJECT_ID OPEN_DATE CLOSE_DATE OWNER
1 8/01/2012 09/01/2012 JEFF
2 8/08/2012 10/01/2012 JEFF
3 9/01/2012 Null JEFF
4 9/12/2012 Null JEFF
5 9/24/2012 11/01/2012 JEFF
6 10/01/2012 12/01/2012 JEFF
7 10/09/2012 01/01/2013 JEFF
我需要这样的 OPEN 计数。完成基本查询后,我需要按项目所有者过滤/分组
AUG12 SEPT12 OCT12 NOV12 DEC12 JAN13 FEB13
2 4 5 4 3 2 1
^ ^ ^ ^ ^ ^ ^
| | | | | | |-> From Projects 3 & 4
| | | | | |-> From Projects 3 & 4
| | | | |-> From Projects 3,4 & 7
| | | |-> From Projects 3,4,6 & 7
| | |-> From Projects 3,4,5,6 & 7
| |-> From Projects 2,3,4, & 5
|-> From Project 1 & 2
最新 这是我根据 Gordon 的建议进行的连接。查询慢约4-5秒
SELECT SUM(case when (OPEN_DATE <= thedate and CLOSE_DATE > thedate) or (OPEN_DATE <= thedate and CLOSE_DATE Is Null) then 1 else 0 end) as Open
1. From (select * FROM Project
2. WHERE Project.Owner = :owner AND Project.action_for = :actionFor )
3. cross join ( select add_months(last_day(SYSDATE), level-7)
4. as thedate from dual connect by level <= 12 )
5. group by to_char(thedate, 'YYYY-MM') order by 1
除了速度慢之外,我不确定它是否能正确处理我的真实数据。我需要分解查询以提供可以手动检查结果的结果。如何调试一条 sql 语句?我的意思是仅仅因为你得到了结果,你怎么知道它们是正确的?