抓住我的头一天。
需要根据与电影相关的分组max()视图,将电影和流派列表缩减为电影和流派列表。
所以转变' a'到''这里有T-SQL
(紫色行是有效的输出表:Image)
+---------+--------------+-------+
| Title | Genre | Views |
+---------+--------------+-------+
| Mad Max | Mockumentary | 1 |
| Mad Max | Sci-fi | 169 |
| Mad Max | Documentary | 32 |
| Titanic | Drama | 6 |
| E.T. | Sci-fi | 34 |
| E.T. | Sci-fi | 2 |
| E.T. | Horror | 1 |
| Taken | Triller | 60 |
| Taken | Crime Drama | 2 |
| Taken | Triller | 40 |
| Taken | Crime Drama | 15 |
+---------+--------------+-------+
预期结果
+---------+---------+-------+
| Title | Genre | Views |
+---------+---------+-------+
| Mad Max | Sci-fi | 169 |
| Titanic | Drama | 6 |
| E.T. | Sci-fi | 36 |
| Taken | Triller | 100 |
+---------+---------+-------+
答案 0 :(得分:2)
试试这个...
SELECT title, genre, views
FROM (SELECT title,
genre,
Sum(views) AS views,
ROW_NUMBER() OVER (PARTITION BY title ORDER BY Sum(views) DESC) AS ranks
FROM tablename
GROUP BY title, genre) tmp
WHERE ranks < 2
输出
+---------+---------+-------+
| title | genre | views |
+---------+---------+-------+
| E.T. | Sci-fi | 36 |
| Mad Max | Sci-fi | 169 |
| Taken | Triller | 100 |
| Titanic | Drama | 6 |
+---------+---------+-------+