如何获得行总和然后获得计算总和的平均值

时间:2019-05-06 02:50:40

标签: sqlite

我有一个代表音乐商店数据的数据库。 我要使用的表是(艺术家,专辑,曲目) 每个专辑都有一个AlbumID,标题和artistID 每个曲目都有TrackID,毫秒,专辑ID和名称

好吧,众所周知,每张专辑都有很多曲目 我要计算专辑中所有歌曲的总和(以毫秒为单位),然后获取该总和的平均值,以获取长度超过平均值的专辑。

我设法计算出每张专辑的长度,但我努力地求出平均值。

我想生成一个像这样的表。

---------------------------------------------------------------------
AlbumID  |  Tile  |  Milliseconds
---------------------------------------------------------------------
1-       |        |
2-       |        |
3-       |        |
...
10-      |        |
----------------------------------------------------------------------

轨道示例表

TrackID  |      Name        | AlbumId | Milliseconds
1   For Those About To Rock      1         343719
6   Put The Finger On You        1         205662
7   Let's Get It Up              1         233926
85  Cochise                      10        222380
86  Show Me How to Live          10        277890
87  Gasoline                     10        279457
88  What You Are                 10        249391
89  Like a Stone                 10        294034
99  Your Time Has Come           11        255529
100 Out Of Exile                 11        291291
101 Be Yourself                  11        279484
102 Doesn't Remind Me            11        255869
111 Money                        12        147591
112 Long Tall Sally              12        106396

所以LIMIT 10 和轨道的长度

1 个答案:

答案 0 :(得分:0)

我相信以下可能是您想要的:-

WITH albumsums(id,asum) AS (
    SELECT albumid, sum(milliseconds) 
    FROM track 
    GROUP BY albumid
)
SELECT album.albumid, album.title, asum 
FROM album 
JOIN albumsums 
    ON album.albumid = albumsums.id  
WHERE asum > (SELECT avg(asum) FROM albumsums)
LIMIT 10;

请考虑以下演示:-

DROP TABLE IF EXISTS track;
DROP TABLE IF EXISTS artist;
DROP TABLE IF EXISTS album;
CREATE TABLE IF NOT EXISTS track (trackid INTEGER PRIMARY KEY, name TEXT, milliseconds INTEGER, albumid);
CREATE TABLE IF NOT EXISTS artist (artistid INTEGER PRIMARY KEY, artistname TEXT);
CREATE TABLE IF NOT EXISTS album (albumid INTEGER PRIMARY KEY, title TEXT, artistid INTEGER);
INSERT INTO artist (artistname) VALUES ('Pink Floyd'),('Genesis'),('Deep Purple');
INSERT INTO album (title,artistid) VALUES('Dark side of the moon',1),('Fireball',3),('Foxtrot',2);
INSERT INTO track (name,milliseconds,albumid) VALUES
    ('Supper''s Ready',((22 * 60) + 57) * 1000,3),
    ('Watcher of the Skies',((7 * 60) + 21) + 1000,3),
    ('Time Table',((4 * 60) + 47) * 1000,3),
    ('Get ''Em Out by Friday',((8 * 60) + 35) * 1000,3),
    ('Can-Utility and the Coastliners',((5 * 60) + 45 ) * 1000,3),
    ('Speak to me /Breath',((3 * 60) + 58) * 1000,1),
    ('On the Run',((3 * 60) + 35) * 1000,1),
    ('Time',((7 * 60) + 5) * 1000,1),
    ('The Great Gig in the Sky',((4 * 60) + 44) * 1000,1),
    ('Money',((6 * 60) + 23) * 1000,1),
    ('Use and Them',((7 * 60) + 50) * 1000,1),
    ('Any Colour you Like',((3 * 60) + 26) * 1000,1),
    ('Brain Damage',((3 * 60) + 47) * 1000,1),
    ('Fireball',((3 * 60) + 24) * 1000,2),
    ('No No No',((6 * 60) + 54) * 1000,2),
    ('Demon''s Eye',((5 * 60) + 21) * 1000,2),
    ('Anyone''s Daughter',((4 * 60) + 43) * 1000,2),
    ('The Mule',((5 * 60) + 21) * 1000,2),
    ('Fools',((8 * 60) + 19) * 1000,2),
    ('No One Came',((6 * 60) + 34) * 1000,1),
    ('Strange Kind of Woman',((4 * 60) + 07) * 1000,1)
;
SELECT * FROM artist;
SELECT * FROM album;
SELECT * FROM track;

SELECT albumid, sum(milliseconds) 
    FROM track 
    GROUP BY albumid
;

WITH albumsums(id,asum) AS (
    SELECT albumid, sum(milliseconds) 
    FROM track 
    GROUP BY albumid
)
SELECT album.albumid, album.title, asum, (SELECT avg(asum) FROM albumsums AS album_average_for_demo)
FROM album 
JOIN albumsums 
    ON album.albumid = albumsums.id  
WHERE asum > (SELECT avg(asum) FROM albumsums);

最终结果(如您所愿,高于平均水平)仅比平均水平高出一张专辑:-

enter image description here

如上一个查询所示,CTE(公用表表达式专辑总和)产生3行(每张专辑1条),其中包含专辑ID和曲目的总和:-

enter image description here

因此,平均唱片集长度为2552147,因此,只有1个唱片集比该唱片集长(如从如此有限的数据量中可以预期的那样)。

表(加载后)为:-

enter image description here enter image description here enter image description here