我是否正确地进行了JOINS,输出不如预期?

时间:2016-11-28 15:31:00

标签: sql oracle

我正在尝试获得所需的输出,如下所示但是我不知道为什么我没有正确地得到它。我不知道为什么我会收到“Date Night”或“Drive Angry”等电影,因为他们在tblReview中没有MovieID。我以为我会在每一行都有CriticUser但是出于某种原因我只在两行输出它。我的平均值也不正确,但其中一些使用科学记数法。

我可以通过哪些方式解决上述问题?

脚本:

SELECT MovieTitle AS "MOVIE", TBLCRITICCLASS.CRITICCLASSDESC AS "Critic Type", AVG(TBLREVIEW.REVIEWSTAR) AS "Average Rating"
FROM TBLMOVIE
FULL JOIN TBLREVIEW ON TBLMOVIE.MOVIEID = TBLREVIEW.MOVIEID
FULL JOIN TBLCRITIC ON TBLREVIEW.CRITICID = TBLCRITIC.CRITICID
FULL JOIN TBLCRITICCLASS ON TBLCRITIC.CRITICID = TBLCRITICCLASS.CRITICCLASSID 
group by MovieTitle, TBLCRITICCLASS.CRITICCLASSDESC
Order by Movietitle;

用脚本输出:

MOVIE                               Critic Type                                        Average Rating
----------------------------------- ------------------------- ---------------------------------------
30 Minutes or Less                                                                            3.8E+00
A Lonely Place to Die               Critic                                                          9
A Lonely Place to Die                                                                         8.7E+00
Abraham Lincoln: Vampire Hunter                                                                      
Act of Valor                                                                                         
Captain America: The First Avenger                                                                   
Date Night                                                                                           
Drive Angry                                                                                          
Saving Private Ryan                                                                                  
Taken                               User                                                            6
Taken                                                                                          6.9375
Taken 2                                                                                           5.6


13 rows selected 

预期产出:

MOVIE                           Critic Type  Average Rating
------------------------        ------------ --------------
30 Minutes or Less              Critic                 2.00
30 Minutes or Less              User                   4.20
A Lonely Place to Die           Critic                10.00
A Lonely Place to Die           User                   8.50
Taken                           Critic                 6.17
Taken                           User                   7.27
Taken 2                         Critic                 4.00
Taken 2                         User                   6.29

8 rows selected

以下是相关文件:

File1 (Pastebin)

File 2 (Pastebin)

1 个答案:

答案 0 :(得分:2)

INNER JOIN开始:

SELECT MovieTitle AS "MOVIE", TBLCRITICCLASS.CRITICCLASSDESC AS "Critic Type", AVG(TBLREVIEW.REVIEWSTAR) AS "Average Rating"
FROM TBLMOVIE INNER JOIN
     TBLREVIEW
     ON TBLMOVIE.MOVIEID = TBLREVIEW.MOVIEID INNER JOIN
     TBLCRITIC
     ON TBLREVIEW.CRITICID = TBLCRITIC.CRITICID INNER JOIN
     TBLCRITICCLASS
     ON TBLCRITIC.CRITICID = TBLCRITICCLASS.CRITICCLASSID 
group by MovieTitle, TBLCRITICCLASS.CRITICCLASSDESC
Order by Movietitle;

您的数据库应具有正确的外键关系,并且您似乎希望在不同的表中匹配行。

如果这不起作用,请检查JOIN条件。例如,这看起来不正确:TBLCRITIC.CRITICID = TBLCRITICCLASS.CRITICCLASSID。但是,您没有描述表格,因此无法确定正确的条件。