data로부터 column에 없는 항목 sql작성하는 법에 관련해서 질문드립니다. 0 5 948

by 머루원해 [SQL Query] [2018.11.07 08:17:16]


20181105_164929.png (23,767Bytes)
award.png (39,981Bytes)
principal.png (37,812Bytes)
movie.png (62,823Bytes)
person.png (45,865Bytes)
genre.png (29,470Bytes)

원문:Eastwood has had played several iconic characters throughout his acting career. Write a subquery that returns the character name, total number of appearances in film, total minutes on film, year of first appearance, and year of last appearance for each character that he has played more than once. In addition, return the average IMDB score, average number of votes, and average profit for the movies that character appears in. Make sure to rename calculated fields that appear in the results.

해석: Eastwood라는 배우는 그의 연기 경력동안 여러 타입의 상징적 인물들을 연기해 왔다.

먼저, 그의 연기경력 동안 그가 한 번이상 연기한 각 캐릭터에 대한 character name(캐릭터 이름), total number of appearances in flim (영화 출연 총 횟 수), total minutes on film (영화 상영 총 분), year of first appearance (첫 출연연도), year of last appearance (마지막 출연연도)를 보여주는 subquery를 만들어라.

그리고, 캐릭터가 출연했던 영화에 대한 the average IMDB Score (평균 평점), average number of votes (평균 투표수), average profit (평균 수익)를 보여주는 쿼리를 만들어라.
 

여기서, 평균 평점, 평균 투표수, 평균 수익은 테이블 column에 나와있어서 구할 수가 있는데, subquery에서 요구하는 영화출연 총횟수, 영화 상영 총분, 첫 출연연도, 마지막 출연연도는 거기에 맞는 table column이 없어서 어떻게 구해야할 지 모르겠네요. 테이블 항목 전부 첨부하겠습니다..

 Character Name은 principal 테이블에서 role이 actor인 것에만 나옵니다. 

테이블 column에서 subquery가 요구하는 항목들을 만들 수 있나요? 

by 마농 [2018.11.07 08:54:44]
SELECT b.CharacterName
     , COUNT(*)                    AS "total number of appearances in flim"
     , SUM(c.RunTime)              AS "total minutes on film"
     , MIN(c.ReleaseYear)          AS "year of first appearance"
     , MAX(c.ReleaseYear)          AS "year of last appearance"
     , AVG(c.Score)                AS "the average IMDB Score"
     , AVG(c.Votes)                AS "average number of votes"
     , AVG(c.BoxOffice - c.Budget) AS "average profit"
  FROM Person a
 INNER JOIN Principal b ON a.PersonID = b.PersonID
 INNER JOIN Movie     c ON b.MovieID  = c.MovieID
 WHERE a.PersonFirst = 'Clint'
   AND a.PersonLast  = 'Eastwood'
   AND b.Role        = 'actor'
 GROUP BY b.CharacterName
HAVING COUNT(*) > 1
;

 


by 머루원해 [2018.11.07 11:36:44]

감사합니다. 알려주신대로 run해보니 

ORA-00972: identifier is too long 이런 오류가 뜨네요.

그 SELECT이랑 FROM사이에 입력된 내용이 많아서 그러는 건가요?

 , COUNT(*)                    AS "total number of appearances in flim"
 , SUM(c.RunTime)              AS "total minutes on film"
 , MIN(c.ReleaseYear)          AS "year of first appearance"
 , MAX(c.ReleaseYear)          AS "year of last appearance"

그러면 이 부분을 subquery로 만든 다음 이걸 넣는 방법도 가능한가요?

즉, 위에 4항목을 한 다음 전체에 넣는 방식이요.
 


by 마농 [2018.11.07 11:47:28]

컬럼명이 너무 길다는 거네요.
알리아스 준거 좀 줄이세요.


by 머루원해 [2018.11.07 12:08:27]

해결됬네요 감사합니다 ㅎㅎ 문제가 애매해서 헷갈리네요 ㅠㅜ


by 마농 [2018.11.07 13:42:50]

글로만 적으면 원래의 의미를 제대로 전달하지 못합니다.
실제 예시자료를 보여주는게 가장 좋은데요.
원본자료 대비 결과자료를 표(테이블) 형태로 보여주고 설명하는게 가장 좋다고 생각됩니다.
그런데 현실은 그렇지 않죠. 모호한 설명만 가지고 쿼리를 작성하라니...

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입