如何查找每三年发布的视频游戏数量?
假设我有一个表videogames,我想找到发布的游戏数量 从 1997 年开始,每隔 3 年进行一次。
视频游戏
videogameid | title | 年份 |
---|---|---|
1 | GoldenEye 007 | 1997 |
2 | Tomb Raider II | 1997 |
3 | Half-Life | 1998 |
4 | The Sims | 2000 |
5 | GTA (III) | 2001 |
6 | Kingdom Hearts | 2003 |
7 | 魔兽世界 | 2004 |
8 | ES4:湮没 | 2006 |
9 | L.A. Noire | 2011 |
10 | Far Cry 3 | 2012 |
11 | Diablo III | 2012 |
从表中可以看出,预期输出应为 Year (1997-1999) = 3, Year (2000-2002) = 2, Year(2003 - 2005) = 2, Year(2006-2008) = 1, Year (2009 - 2011 ) = 1 and Year (2012-2014)= 2
这是我解决代码的尝试:
SELECT COUNT(videogameid) AS number_of_videogames
FROM videogames
WHERE INTERVAL(1997,2,2,2,2,2,2)
GROUP BY YEAR;
出于某种原因,我得到了返回超过 100 ++ 行的答案,而每个间隔应该只有 6 行。
Lets say i have a table videogames and I want to find the number of games released in
in intervals of 3 years starting from year 1997.
videogames
videogameid | title | year |
---|---|---|
1 | GoldenEye 007 | 1997 |
2 | Tomb Raider II | 1997 |
3 | Half-Life | 1998 |
4 | The Sims | 2000 |
5 | GTA (III) | 2001 |
6 | Kingdom Hearts | 2003 |
7 | World Of Warcraft | 2004 |
8 | ES4: Oblivion | 2006 |
9 | L.A. Noire | 2011 |
10 | Far Cry 3 | 2012 |
11 | Diablo III | 2012 |
From the table, the expected output should be Year (1997-1999) = 3, Year (2000-2002) = 2,
Year(2003 - 2005) = 2, Year(2006-2008) = 1, Year (2009 - 2011 ) = 1 and Year (2012-2014)= 2
This is my attempt at solving the code:
SELECT COUNT(videogameid) AS number_of_videogames
FROM videogames
WHERE INTERVAL(1997,2,2,2,2,2,2)
GROUP BY YEAR;
For some reason, I got returned back more than 100 ++ rows of answers when they should only be 6 rows for each interval.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新 2022-03-06
列出没有参考年份数字的总数似乎很奇怪:-),但如果这就是您想要的,请尝试:
结果:
原始答案
如果您想以“(最小-最大)”格式显示年份:
结果:
db<>fiddle 此处
Update 2022-03-06
It seems strange to list the totals with no reference year numbers :-) but if that's all you want, try:
Results:
Original Answer
If you want to display the year in format "(min-max)":
Results:
db<>fiddle here
我认为你能得到的最简单的方法是使用递归 CTE 来生成你的年份,然后简单地将其外部连接到你的源数据并分组为 3 个,如下所示:
演示DB<>Fiddle
About the simplest I think you can get is to use a recursive CTE to generate your years and simply outer-join this to your source data and group into 3 like so:
Demo DB<>Fiddle