【问题标题】:Writing an SQL statement that references a nested VARRAY编写引用嵌套 VARRAY 的 SQL 语句
【发布时间】:2018-10-02 02:32:22
【问题描述】:

我正在尝试编写一个 SQL 语句,在不使用 PL/SQL 的情况下查询名为 movie 的表中的列 actors(定义为 VARRAY(5)),以回答问题:

列出出现在多部电影中的所有演员,显示电影名称和演员

select a.column_value, count(*), m.title
from movie m, table (m.actors) a
where count(*) > 2,
group by a.column_value;

【问题讨论】:

  • edit您的问题,并为表movie、一些sample data 和基于该数据的预期输出添加create table 语句。 Formatted textno screen shots。 (edit 您的问题,请点击问题下方的edit 链接。请勿在 cmets 中发布代码或其他信息)

标签: sql oracle varray


【解决方案1】:

您应该使用HAVING 子句而不是where 子句来检查条件,另外LISTAGG 可用于显示电影标题。

CREATE OR REPLACE TYPE actortype IS VARRAY(5) OF VARCHAR2(20);
/

create table movie ( id integer , title VARCHAR2(100), actors actortype );

INSERT INTO movie (
     id,
     title,
     actors
) VALUES (
     1,
     'The Shawshank Redemption',
     actortype('Morgan Freeman','Tim Robbins','Bob Gunton')
);
INSERT INTO movie (
     id,
     title,
     actors
) VALUES (
     1,
     'The Dark Knight',
     actortype('Morgan Freeman','Christian Bale','Heath Ledger')
);

查询

SELECT a.column_value as Actor,
       COUNT(*) as num_of_movies,
       LISTAGG( m.title, ',') WITHIN GROUP ( ORDER BY id ) as movies
FROM movie m,
     TABLE ( m.actors ) a
GROUP BY a.column_value
HAVING COUNT(*) > 1;

ACTOR          NUM_OF_MOVIES MOVIES
------------  -------------  -------
Morgan Freeman  2            The Dark Knight,The Shawshank Redemption

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-14
    • 2021-09-27
    • 2011-03-12
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    相关资源
    最近更新 更多