如何查找每三年发布的视频游戏数量?

发布于 2025-01-12 04:02:31 字数 1246 浏览 1 评论 0原文

假设我有一个表videogames,我想找到发布的游戏数量 从 1997 年开始,每隔 3 年进行一次。

视频游戏

videogameidtitle年份
1GoldenEye 0071997
2Tomb Raider II1997
3Half-Life1998
4The Sims2000
5GTA (III)2001
6Kingdom Hearts2003
7魔兽世界2004
8ES4:湮没2006
9L.A. Noire2011
10Far Cry 32012
11Diablo III2012

从表中可以看出,预期输出应为 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

videogameidtitleyear
1GoldenEye 0071997
2Tomb Raider II1997
3Half-Life1998
4The Sims2000
5GTA (III)2001
6Kingdom Hearts2003
7World Of Warcraft2004
8ES4: Oblivion2006
9L.A. Noire2011
10Far Cry 32012
11Diablo III2012

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄离 2025-01-19 04:02:31

更新 2022-03-06

列出没有参考年份数字的总数似乎很奇怪:-),但如果这就是您想要的,请尝试:

WITH recursive ReleaseYears AS (
  -- build list of years 
  SELECT MIN(`year`) AS YearNum
  FROM   VideoGames
  UNION ALL
  SELECT YearNum + 1
  FROM   ReleaseYears
  WHERE  YearNum <= ( SELECT MAX(`year`) + 2 FROM VideoGames) 
)
SELECT COUNT(vg.videogameid) AS Num
FROM   ( -- group year numbers in groups of 3 
         SELECT FLOOR((ROW_NUMBER() OVER(ORDER BY (SELECT YearNum)) -1) / 3) + 1 AS YearGroup
               , YearNum
         FROM   ReleaseYears
       ) y LEFT JOIN VideoGames vg ON y.YearNum = vg.`year`
GROUP BY YearGroup       
HAVING COUNT(vg.videogameid) > 0

结果:

<前>|数量 |
| --: |
| 3 |
| 2 |
| 2 |
| 1 |
| 1 |


原始答案

如果您想以“(最小-最大)”格式显示年份:

WITH recursive ReleaseYears AS (
  SELECT MIN(`year`) AS YearNum
  FROM   VideoGames
  UNION ALL
  SELECT YearNum + 1
  FROM   ReleaseYears
  WHERE  YearNum <= ( SELECT MAX(`year`) + 2 FROM VideoGames) 
)
SELECT YearGroup
       , CONCAT_WS('-', MIN(YearNum), MAX(YearNum)) AS YearRange
       , COUNT(vg.videogameid) AS Num
FROM   (
         SELECT FLOOR((ROW_NUMBER() OVER(ORDER BY (SELECT YearNum)) -1) / 3) + 1 AS YearGroup
               , YearNum
         FROM   ReleaseYears
       ) y LEFT JOIN VideoGames vg ON y.YearNum = vg.`year`
GROUP BY YearGroup       
HAVING COUNT(vg.videogameid) > 0
;

结果:

年级 |年份范围 |数量
--------:| :-------- | --:
        1 | 1997-1999 | 3
        2 | 2000-2002 | 2
        3 | 2003-2005 | 2
        4 | 2006-2008 | 1
        5 | 2009-2011 | 1
        6 | 2012-2014 | 2

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:

WITH recursive ReleaseYears AS (
  -- build list of years 
  SELECT MIN(`year`) AS YearNum
  FROM   VideoGames
  UNION ALL
  SELECT YearNum + 1
  FROM   ReleaseYears
  WHERE  YearNum <= ( SELECT MAX(`year`) + 2 FROM VideoGames) 
)
SELECT COUNT(vg.videogameid) AS Num
FROM   ( -- group year numbers in groups of 3 
         SELECT FLOOR((ROW_NUMBER() OVER(ORDER BY (SELECT YearNum)) -1) / 3) + 1 AS YearGroup
               , YearNum
         FROM   ReleaseYears
       ) y LEFT JOIN VideoGames vg ON y.YearNum = vg.`year`
GROUP BY YearGroup       
HAVING COUNT(vg.videogameid) > 0

Results:

| Num |
| --: |
|   3 |
|   2 |
|   2 |
|   1 |
|   1 |

Original Answer

If you want to display the year in format "(min-max)":

WITH recursive ReleaseYears AS (
  SELECT MIN(`year`) AS YearNum
  FROM   VideoGames
  UNION ALL
  SELECT YearNum + 1
  FROM   ReleaseYears
  WHERE  YearNum <= ( SELECT MAX(`year`) + 2 FROM VideoGames) 
)
SELECT YearGroup
       , CONCAT_WS('-', MIN(YearNum), MAX(YearNum)) AS YearRange
       , COUNT(vg.videogameid) AS Num
FROM   (
         SELECT FLOOR((ROW_NUMBER() OVER(ORDER BY (SELECT YearNum)) -1) / 3) + 1 AS YearGroup
               , YearNum
         FROM   ReleaseYears
       ) y LEFT JOIN VideoGames vg ON y.YearNum = vg.`year`
GROUP BY YearGroup       
HAVING COUNT(vg.videogameid) > 0
;

Results:

YearGroup | YearRange | Num
--------: | :-------- | --:
        1 | 1997-1999 |   3
        2 | 2000-2002 |   2
        3 | 2003-2005 |   2
        4 | 2006-2008 |   1
        5 | 2009-2011 |   1
        6 | 2012-2014 |   2

db<>fiddle here

波浪屿的海角声 2025-01-19 04:02:31

我认为你能得到的最简单的方法是使用递归 CTE 来生成你的年份,然后简单地将其外部连接到你的源数据并分组为 3 个,如下所示:

with recursive n as (
    select Min(year) yn, Ceiling((max(year)-Min(year))/3.0)*3 + min(year) maxyear, Min(year) minyear 
    from t
    union all
    select yn + 1, maxyear, minyear
    from n
    where yn < maxyear
)
select Min(yn) FromYear, Max(yn) toYear, Count(year) qty
from n
left join t on t.year=yn
group by floor((yn - minyear) /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:

with recursive n as (
    select Min(year) yn, Ceiling((max(year)-Min(year))/3.0)*3 + min(year) maxyear, Min(year) minyear 
    from t
    union all
    select yn + 1, maxyear, minyear
    from n
    where yn < maxyear
)
select Min(yn) FromYear, Max(yn) toYear, Count(year) qty
from n
left join t on t.year=yn
group by floor((yn - minyear) /3);

Demo DB<>Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文