这些是表格信息。
CREATE TABLE actor(
id INTEGER NOT NULL,
name VARCHAR2(100),
CONSTRAINT actor_PK PRIMARY KEY (id));
CREATE TABLE movie(
id INTEGER NOT NULL,
title VARCHAR2(100),
yr NUMBER(4,0),
score NUMBER,
votes INTEGER,
director VARCHAR2(100),
CONSTRAINT movie_PK PRIMARY KEY (id));
CREATE TABLE casting(
movie_id INTEGER NOT NULL,
actor_id INTEGER NOT NULL,
ord INTEGER,
CONSTRAINT casting_PK PRIMARY KEY (movie_id, actor_id),
CONSTRAINT casting_FK1 FOREIGN KEY (movie_id) REFERENCES movie(id),
CONSTRAINT casting_FK2 FOREIGN KEY (actor_id) REFERENCES actor(id));
我如何找到用作电影标题中第一个单词的前5个热门单词? (不包括“ A”,“ An”,“ The”和“ I”)。 按从高到低的顺序列出单词。您可以 忽略只有一个单词的电影标题。
答案 0 :(得分:0)
此简单方法将查找第一次出现的空白(仅在带有空白的标题中)。它将按丢弃列表的出现次数降序排列(“ A”,“ An”,“ The”,“ I”)
with words AS (
SELECT
substr(m.title, 1, instr(m.title, ' ')) AS firstword,
COUNT(*) AS counter
FROM
movie m
WHERE
instr(m.title, ' ') > 0
GROUP BY
substr(m.title, 1, instr(m.title, ' '))
)
SELECT
w.firstword,
w.counter
FROM
words w
where UPPER(w.firstword) not in ('A', 'AN', 'THE', 'I')
ORDER BY
w.counter DESC
如果不区分大小写:
with words AS (
SELECT
UPPER(substr(m.title, 1, instr(m.title, ' '))) AS firstword,
COUNT(*) AS counter
FROM
movie m
WHERE
instr(m.title, ' ') > 0
GROUP BY
UPPER(substr(m.title, 1, instr(m.title, ' ')))
)
SELECT
w.firstword,
w.counter
FROM
words w
where w.firstword not in ('A', 'AN', 'THE', 'I')
ORDER BY
w.counter DESC