如何计算存储在3个不同列中的每种类型?

huangapple go评论57阅读模式
英文:

How to count each genre stored in 3 different columns?

问题

我有一个家庭电影数据库,从IMDB上爬取的。电影的类型存储在三个单独的列中(一些电影只有一个类型,所以其他两个列的值为NULL,而其他电影可能三个列都有值)。

例如:

Extraordinary Measures	2010	6.4	106	Drama		                Drama	NULL	NULL
Away We Go	        2009	7	98	Adventure, Comedy, Drama	Adventure Comedy Drama
Repo Men	        2010	6.3	111	Action, Sci-Fi, Thriller	Action	 Sci-Fi	 Thriller
The Ides of March	2011	7.1	101	Drama, Thriller	                Drama	 Thriller	NULL

我想在Tableau中创建一个可视化,显示我每年拥有的每种类型的电影数量。

我想知道如何创建一个查询,以返回每种类型的电影数量。

我尝试使用 GROUP BYCOUNT

此外,我在考虑使用 CASE,但我觉得这可能会变得非常复杂,而且很难跟踪,所以是否有一些内置的函数可以帮助我解决这个问题?

谢谢!

英文:

I have a home movie database, scraped from IMDB. The genres of the movies are stored in 3 separate columns (some movies only have 1, so the other two are NULL, while others have all 3 occupied).

For example:

Extraordinary Measures	2010	6.4	106	Drama		                Drama	NULL	NULL
Away We Go	        2009	7	98	Adventure, Comedy, Drama	Adventure Comedy Drama
Repo Men	        2010	6.3	111	Action, Sci-Fi, Thriller	Action	 Sci-Fi	 Thriller
The Ides of March	2011	7.1	101	Drama, Thriller	                Drama	 Thriller	NULL

I want to create a visual in Tableau showing the number of movies from each genre I have from each year.

I was wondering how I could create a query which would return the number of each genre.

I tried using GROUP BY, and COUNT.

Also I was thinking about using CASE but I feel like it'd be really complicated and hard to keep track of, so is there some built-in function to help me with my issue?

Thank you!

答案1

得分: 2

这可以通过使用 union all 将所有流派列合并为一列,然后对结果数据集应用聚合函数 count() 来实现:

select year, genre, count(1)
from (
  select year, genre1 as genre
  from mytable
  union all
  select year, genre2
  from mytable
  union all
  select year, genre3
  from mytable
) as s
where genre is not null
group by year, genre
英文:

This can be accomplished by combining all genre columns into one column using union all and then applying the aggregate function count() to the resulting dataset:

select year, genre, count(1)
from (
  select year, genre1 as genre
  from mytable
  union all
  select year, genre2
  from mytable
  union all
  select year, genre3
  from mytable
) as s
where genre is not null
group by year, genre

huangapple
  • 本文由 发表于 2023年6月26日 00:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定