2012-03-07 19 views
0

このタスクでは、50以上の映画に参加した俳優の名前をすべて見つけるはずです。すべての映画について、映画の中の他の俳優とアルファベットで姓が最初に来ていることを確認する必要があります。ここで SQLインデックスの検索

select 
    lastname, firstname, count(*) as films 
from 
    (select 
      distinct p.lastname, p.firstname, f.filmid 
     from 
      person p, filmparticipation f, filmitem i, film fi 
     where 
      fi.filmid = i.filmid and 
      i.filmid = f.filmid and 
      p.personid = f.personid and 
      f.parttype = 'cast' and filmtype = 'C') person 
group by 
    lastname, firstname 
having 
    count(*) >= 450 
order by 
    lastname, firstname desc; 

は、いくつかのfilmtypesに関する

create table person(
personid int primary key, 
lastname text not null, 
firstname text not null, 
gender(1), 
); 
create index personlastnameindex on person (lastname); 

create table filmparticipation (
partid int primary key, 
personid int not null references person (personid), 
filmid int not null references filmitem (filmid), 
parttype text not null 
); 

create index filmparticipationpersonidindex on filmparticipation (personid); 
create index filmparticipationpersonidindex on filmparticipation (filmid); 

create table film(
filmid int primary key references filmitem (filmid), 
title text not null, 
prodyear int 
); 

create index filmtitleindex on film (title); 
create index filmyearindex on film (prodyear); 

一部explainasionsクエリに関するいくつかの関連するテーブルです=>

C => cinema movie V => videomovie VG => videogame TV => TV-film..... 

私は実際にそれは人が最初に来ていることを確認しますするにはどうすればよいですアルファベットと他の俳優の違いは?

例=>ムービーパルプフィクション...(他にもいくつかの俳優がありますが、主要な文字を取ります) Samuel L. Jackson ||ジョントラボルタジャクソンがトラボルタの前に来るのを見て簡単なことだから、ジャクソン等のために+1になるよ...

私は、その映画の中のp.lastnameのような擬似コードを見たことがある。 lastname < =すべて(映画の中の最後の名前)

どのようにしてどちらを使うことができますか?それとも良い方法ですか?

+0

'filmitem'表とは何?そのテーブルの作成を見ることはできません。 –

+0

リストの最初の俳優(映画の俳優リストが姓で注文されている場合)を数えたいだけですか? – Eggi

+0

ムービーにはキャストリストがあります。そのムービーのキャスティングリストがあれば、私はそのリストからmin(p.lastname)を取得して何とかカウントするように提案しました – CptHindsight

答えて

0

あなたがオーバーROW_NUMBER()を含んでRDBMSを使用していると仮定すると、試してみてください。

select lastname, 
     firstname, 
     count(distinct filmid) as films, 
     count(distinct case when rn=1 then filmid end) as alpha_first 
from 
(select p.lastname, 
     p.firstname, 
     f.filmid, 
     row_number() over (partition by f.filmid order by lastname, firstname) rn 
from person p, filmparticipation f, filmitem i, film fi 
where fi.filmid = i.filmid and 
     i.filmid = f.filmid and 
     p.personid = f.personid and 
     f.parttype = 'cast' and filmtype = 'C') person 
group by lastname, firstname 
having count(distinct filmid) >= 50 
order by lastname, firstname desc; 
関連する問題