Search code examples
sqlsql-serverunpivotimdb

Unpivoting movie genres


I'm looking at some movie data from IMDb and I have found a limitation in my knowledge. Here is the data I'm looking at:

enter image description here

I think I will run into problems with this current table. How can I get it so that there are two columns, MovieID and Genre? It would then cause MovieID to not be distinct. Would this require unpivoting?

Any help would be much appreciated!


Solution

  • I can't see your screenshot so I'm guessing at the columns, but something along the lines of:

    SELECT MovieID, "Horror" As Genre
    WHERE Horror = 1
    UNION ALL
    SELECT MovieID, "Comedy" As Genre
    WHERE Comedy = 1
    UNION ALL
    SELECT MovieID, "Drama" As Genre
    WHERE Drama = 1
    UNION ALL
    ...