Oracle SELECT:每个团队每年的事件计数
正如标题中所述,我想选择每个团队和每年的赛事数量。下面的 select 语句工作正常,但没有给出我想要的内容。
SELECT
Team.team_id,
TO_CHAR(Event.START_DATE_TIME, 'yyyy') AS year,
count(event_id) AS events
FROM
Team
LEFT OUTER JOIN
Event ON Event.team_id = Team.team_id
GROUP BY
TO_CHAR(Event.START_DATE_TIME, 'yyyy'),
team_id
ORDER BY
year ASC,
team_id ASC
;
有了这个,如果我们有:
Team 1 : 1 event in 2006
Team 2 : 1 event in 2007
我们获得:
ID | Year | Events
------------------
1 | 2006 | 1
2 | 2007 | 1
我想获得:
ID | Year | Events
-------------------
1 | 2006 | 1
2 | 2006 | 0
1 | 2007 | 0
2 | 2007 | 1
我不知道如何修改我的请求来这样做。
As said in the title, I want to select the number of events per team and per year. The select statement below works fine but doesn't give me exactly what I am looking for.
SELECT
Team.team_id,
TO_CHAR(Event.START_DATE_TIME, 'yyyy') AS year,
count(event_id) AS events
FROM
Team
LEFT OUTER JOIN
Event ON Event.team_id = Team.team_id
GROUP BY
TO_CHAR(Event.START_DATE_TIME, 'yyyy'),
team_id
ORDER BY
year ASC,
team_id ASC
;
With this, if we have :
Team 1 : 1 event in 2006
Team 2 : 1 event in 2007
We obtain :
ID | Year | Events
------------------
1 | 2006 | 1
2 | 2007 | 1
And I would like to obtain :
ID | Year | Events
-------------------
1 | 2006 | 1
2 | 2006 | 0
1 | 2007 | 0
2 | 2007 | 1
I don't know how to modify my request to do so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以它有效! :)
这是我的最后一个询问:
谢谢!
So it works ! :)
Here is my final query :
Thank you !
我这里没有 Oracle 来测试,但一般来说这应该有效:
您将获得每年的结果,其中您有活动中的活动。如果这还不够,您可以将 Years 子项替换为包含所有年份的表格。
I have not Oracle here to test, but in general this should work:
You will get results for every year, where you have an event in the events. If that's not enough, you can replace the Years sub with a table filled with all the years.
使用:
这将生成一个年份列表,但您必须设置
:start_year
和:end_year
BIND 变量。以前:
TO_CHAR 可以获取年份,但将其作为 CHAR(4) 返回,因此您需要使用
TO_NUMBER(TO_CHAR(date_col, 'yyyy'))
来获取数字。所以我使用 EXTRACT 代替......Use:
This will generate a list of years, but you have to set the
:start_year
and:end_year
BIND variables.Previously:
TO_CHAR is OK to get the year, but returns it as a CHAR(4) so you need to use
TO_NUMBER(TO_CHAR(date_col, 'yyyy'))
to get a number. So I used EXTRACT instead...