当 运行 在 Sqlite 中查询时,我得到的答案是 9.9。但这不是正确答案
When running the query in Sqlite, I get the answer as 9.9. But that is not the correct answer
[https://www.sqlitetutorial.net/sqlite-sample-database/]
我写这段代码是为了得到 'What is the total price for the album “Big Ones”?'
的答案
SELECT al.Title, i.Total, tr.albumid, SUM(tr.UnitPrice)
FROM ((((artists ar INNER JOIN albums al ON ar.ArtistId = al.ArtistId)
INNER JOIN tracks tr ON al.AlbumId = tr.AlbumId)
INNER JOIN invoice_items ii ON tr.TrackId = ii.TrackId)
INNER JOIN invoices i ON ii.invoiceid = i.invoiceid)
WHERE al.Title = 'Big Ones'
我得到 SUM(tr.UnitPrice)
作为 9.9
。
但是9.9是错误的。正确答案应该是什么?
使用了 Chinook 数据库中的表。 link如上所示。
您必须对专辑所有曲目的单价求和,所以只需要2张表:albums
和tracks
。
加入他们,汇总:
select a.title,
sum(t.unitprice) total_price
from albums a inner join tracks t
on t.albumid = a.albumid
where a.title = 'Big Ones'
group by a.title
即使省略 group by
子句也可以获得相同的结果。
select
Title,
sum(UnitPrice)
from albums
join tracks
on albums.AlbumId = tracks.AlbumId
where albums.Title = 'Big Ones'
[https://www.sqlitetutorial.net/sqlite-sample-database/]
我写这段代码是为了得到 'What is the total price for the album “Big Ones”?'
的答案SELECT al.Title, i.Total, tr.albumid, SUM(tr.UnitPrice)
FROM ((((artists ar INNER JOIN albums al ON ar.ArtistId = al.ArtistId)
INNER JOIN tracks tr ON al.AlbumId = tr.AlbumId)
INNER JOIN invoice_items ii ON tr.TrackId = ii.TrackId)
INNER JOIN invoices i ON ii.invoiceid = i.invoiceid)
WHERE al.Title = 'Big Ones'
我得到 SUM(tr.UnitPrice)
作为 9.9
。
但是9.9是错误的。正确答案应该是什么?
使用了 Chinook 数据库中的表。 link如上所示。
您必须对专辑所有曲目的单价求和,所以只需要2张表:albums
和tracks
。
加入他们,汇总:
select a.title,
sum(t.unitprice) total_price
from albums a inner join tracks t
on t.albumid = a.albumid
where a.title = 'Big Ones'
group by a.title
即使省略 group by
子句也可以获得相同的结果。
select
Title,
sum(UnitPrice)
from albums
join tracks
on albums.AlbumId = tracks.AlbumId
where albums.Title = 'Big Ones'