如何从查询中获得结果的总和?

时间:2013-09-20 23:20:14

标签: sql sqlite sum

我想知道如何能够提取多少导演,导演电影或导演指导了多少部电影。例如,我有李安导演两部电影。我想知道如何提取这些信息。我想知道如何查询他总共指导了两部电影。我感兴趣的另一类信息是The Matrix有两位董事。我将如何查询The Matrix有两名董事。这是我的代码:

CREATE TABLE MOVIE(
    MId INTEGER,
    Title VARCHAR(50),
    ReleaseDate DATE,
    Budget DOUBLE,
    PRIMARY KEY (MId)
);


CREATE TABLE DIRECTOR(
    DId INTEGER,
    Name VARCHAR(30),
    DateOfBirth DATE,
    PRIMARY KEY (DId)
);

CREATE TABLE DIRECTED(
    DirectorId INTEGER,
    MovieId INTEGER,
    PRIMARY KEY(DirectorId, MovieId),
    FOREIGN KEY(DirectorId) REFERENCES DIRECTOR(DId),
    FOREIGN KEY(MovieId) REFERENCES MOVIE(MId)
);

INSERT INTO MOVIE VALUES(1, 'Life of Pi', '2012-11-21', 120000000);
INSERT INTO MOVIE VALUES(3, 'Argo', '2012-10-12', 44500000);
INSERT INTO MOVIE VALUES(7, 'The Dark Knight Rises', '2012-07-20', 250000000);
INSERT INTO MOVIE VALUES(15, 'Thor', '2011-12-10', 150000000);
INSERT INTO MOVIE VALUES(45, 'The Sessions', '2012-01-23', 1000000);
INSERT INTO MOVIE VALUES(65, 'Batman Begins', '2005-06-15', 150000000);
INSERT INTO MOVIE VALUES(86, 'Goodfellas', '1990-09-19', 25000000);
INSERT INTO MOVIE VALUES(95, 'The Departed', '2006-10-06', 90000000);
INSERT INTO MOVIE VALUES(103, 'There's Something about Mary', '1998-07-15', 23000000);
INSERT INTO MOVIE VALUES(123, 'The Matrix', '1999-05-31', 63000000);
INSERT INTO MOVIE VALUES(144, 'Lust, Caution', '2007-09-28', 15000000);

INSERT INTO DIRECTOR VALUES(4, 'Ang Lee', '1954-10-23');
INSERT INTO DIRECTOR VALUES(5, 'Ben Affleck', '1972-08-15');
INSERT INTO DIRECTOR VALUES(8, 'Christopher Nolan', '1970-07-30');
INSERT INTO DIRECTOR VALUES(9, 'Kenneth Branagh', '1960-12-10');
INSERT INTO DIRECTOR VALUES(13, 'Ben Lewin', '1946-01-23');
INSERT INTO DIRECTOR VALUES(23, 'Martin Scorsese', '1942-11-17');
INSERT INTO DIRECTOR VALUES(25, 'Christopher Nolan', '1970-07-30');
INSERT INTO DIRECTOR VALUES(33, 'Peter Farrelly', '1956-12-17');
INSERT INTO DIRECTOR VALUES(34, 'Robert Farrelly', '1958-06-17');
INSERT INTO DIRECTOR VALUES(45, 'Lana Wachowski', '1965,06-21');
INSERT INTO DIRECTOR VALUES(46, 'Andrew Wachowski', '1967-12-29');

INSERT INTO DIRECTED VALUES(4, 1);
INSERT INTO DIRECTED VALUES(5, 3);
INSERT INTO DIRECTED VALUES(8, 7);
INSERT INTO DIRECTED VALUES(9, 15);
INSERT INTO DIRECTED VALUES(13, 45);
INSERT INTO DIRECTED VALUES(25, 65);
INSERT INTO DIRECTED VALUES(23, 86);
INSERT INTO DIRECTED VALUES(23, 95);
INSERT INTO DIRECTED VALUES(33, 103);
INSERT INTO DIRECTED VALUES(34, 103);
INSERT INTO DIRECTED VALUES(45, 123);
INSERT INTO DIRECTED VALUES(46, 123);
INSERT INTO DIRECTED VALUES(4, 144);

2 个答案:

答案 0 :(得分:2)

您需要JOINGROUP BYCOUNT()聚合

SELECT d.did, d.name director_name, COUNT(*) number_of_films
  FROM director d LEFT JOIN directed r
    ON d.did = r.directorid
 GROUP BY d.did, d.name

输出:

| DId |     director_name | number_of_films |
|-----|-------------------|-----------------|
|   4 |           Ang Lee |               2 |
|   5 |       Ben Affleck |               1 |
|   8 | Christopher Nolan |               1 |
|   9 |   Kenneth Branagh |               1 |
|  13 |         Ben Lewin |               1 |
|  23 |   Martin Scorsese |               2 |
|  25 | Christopher Nolan |               1 |
|  33 |    Peter Farrelly |               1 |
|  34 |   Robert Farrelly |               1 |
|  45 |    Lana Wachowski |               1 |
|  46 |  Andrew Wachowski |               1 |

获得有多个导演的电影

SELECT m.*, number_of_directors
  FROM 
(
  SELECT movieid, COUNT(*) number_of_directors
    FROM directed r
   GROUP BY movieid
  HAVING number_of_directors > 1
) q JOIN movie m
    ON q.movieid = m.mid

输出:

| MId |                        Title | ReleaseDate |   Budget | number_of_directors |
|-----|------------------------------|-------------|----------|---------------------|
| 103 | There's Something about Mary |  1998-07-15 | 23000000 |                   2 |
| 123 |                   The Matrix |  1999-05-31 | 63000000 |                   2 |

这是 SQLFiddle 演示

答案 1 :(得分:1)

select Name, count(DId)
from DIRECTED d inner join DIRECTOR m on d.DirectorId = m.DId
inner join MOVIE m on d.MovieId = m.mId
group by m.Title 

http://sqlfiddle.com/#!5/28347/4/0

select m.Title, count(DId)
from DIRECTED d inner join DIRECTOR m on d.DirectorId = m.DId
inner join MOVIE m on d.MovieId = m.mId
group by Name 

http://sqlfiddle.com/#!5/28347/6/0