组合两个单独工作的 sqlite 查询

发布于 2025-01-10 21:05:35 字数 2320 浏览 0 评论 0原文

我正在研究基本的 chinook 数据库,并尝试编写一个 sqlite 查询来为 10 位畅销艺术家创建一个名为 v10BestSellingArtists 的视图,该视图基于按 TotalTrackSales 降序排列的已售曲目总数(名为 TotalTrackSales)。 TotalAlbum 是每位艺术家售出的包含曲目的专辑数量。

我可以分别为它们两个查询编写查询,但我不知道如何合并这两个查询:

用于查找 Totaltracksales 的查询:

Select 
r.name as artist, 
count (i.quantity) as TotalTrackSales 
from albums a 
left join tracks t on t.albumid == a.albumid 
left join invoice_items i on i.trackid == t.trackid 
left join artists r on a.artistid == r.artistid 
group by r.artistid 
order by 2 desc 
limit 10;

以及用于 TotalAlbum 的第二个查询:

Select 
r.name as artist, 
count(a.artistId) from albums a 
left join artists r where a.artistid == r.artistid 
group by a.artistid 
order by 2 desc 
limit 10;

但我想要一个包含以下列的查询:Artist、TotalAlbum TotalTrackSales。

任何帮助表示赞赏。

专辑表的架构:

 [Title] NVARCHAR(160)  NOT NULL,
 [ArtistId] INTEGER  NOT NULL,
 FOREIGN KEY ([ArtistId]) REFERENCES "artists" ([ArtistId])

艺术家表:

[ArtistId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[Name] NVARCHAR(120)

曲目表架构:

 [TrackId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    [Name] NVARCHAR(200)  NOT NULL,
    [AlbumId] INTEGER,
    [MediaTypeId] INTEGER  NOT NULL,
    [GenreId] INTEGER,
    [Composer] NVARCHAR(220),
    [Milliseconds] INTEGER  NOT NULL,
    [Bytes] INTEGER,
    [UnitPrice] NUMERIC(10,2)  NOT NULL,
    FOREIGN KEY ([AlbumId]) REFERENCES "albums" ([AlbumId])
                ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([GenreId]) REFERENCES "genres" ([GenreId])
                ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([MediaTypeId]) REFERENCES "media_types" ([MediaTypeId])
                ON DELETE NO ACTION ON UPDATE NO ACTION

表invoice_items:

    [InvoiceLineId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    [InvoiceId] INTEGER  NOT NULL,
    [TrackId] INTEGER  NOT NULL,
    [UnitPrice] NUMERIC(10,2)  NOT NULL,
    [Quantity] INTEGER  NOT NULL,
    FOREIGN KEY ([InvoiceId]) REFERENCES "invoices" ([InvoiceId])
                ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([TrackId]) REFERENCES "tracks" ([TrackId])
                ON DELETE NO ACTION ON UPDATE NO ACTION

I am working on the basic chinook database, and am trying to write a sqlite query to create a view called v10BestSellingArtists for the 10 bestselling artists based on the total quantity of tracks sold (named as TotalTrackSales) order by TotalTrackSales in descending order. TotalAlbum is the number of albums with tracks sold for each artist.

I can write queries for both of them separately, but I can't figure out how to merge these two queries:

query for finding Totaltracksales:

Select 
r.name as artist, 
count (i.quantity) as TotalTrackSales 
from albums a 
left join tracks t on t.albumid == a.albumid 
left join invoice_items i on i.trackid == t.trackid 
left join artists r on a.artistid == r.artistid 
group by r.artistid 
order by 2 desc 
limit 10;

and the second query for totalAlbum :

Select 
r.name as artist, 
count(a.artistId) from albums a 
left join artists r where a.artistid == r.artistid 
group by a.artistid 
order by 2 desc 
limit 10;

but I want one query with the columns: Artist, TotalAlbum TotalTrackSales.

Any help is appreciated.

The schema for the album table:

 [Title] NVARCHAR(160)  NOT NULL,
 [ArtistId] INTEGER  NOT NULL,
 FOREIGN KEY ([ArtistId]) REFERENCES "artists" ([ArtistId])

artists table :

[ArtistId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[Name] NVARCHAR(120)

tracks table schema:

 [TrackId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    [Name] NVARCHAR(200)  NOT NULL,
    [AlbumId] INTEGER,
    [MediaTypeId] INTEGER  NOT NULL,
    [GenreId] INTEGER,
    [Composer] NVARCHAR(220),
    [Milliseconds] INTEGER  NOT NULL,
    [Bytes] INTEGER,
    [UnitPrice] NUMERIC(10,2)  NOT NULL,
    FOREIGN KEY ([AlbumId]) REFERENCES "albums" ([AlbumId])
                ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([GenreId]) REFERENCES "genres" ([GenreId])
                ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([MediaTypeId]) REFERENCES "media_types" ([MediaTypeId])
                ON DELETE NO ACTION ON UPDATE NO ACTION

table invoice_items:

    [InvoiceLineId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    [InvoiceId] INTEGER  NOT NULL,
    [TrackId] INTEGER  NOT NULL,
    [UnitPrice] NUMERIC(10,2)  NOT NULL,
    [Quantity] INTEGER  NOT NULL,
    FOREIGN KEY ([InvoiceId]) REFERENCES "invoices" ([InvoiceId])
                ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([TrackId]) REFERENCES "tracks" ([TrackId])
                ON DELETE NO ACTION ON UPDATE NO ACTION

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2025-01-17 21:05:35

为了合并您的 2 个查询,您可以使用 CTE 执行以下操作:

with total_track_sales as (
Select 
r.name as artist, 
count (i.quantity) as TotalTrackSales 
from albums a 
left join tracks t on t.albumid == a.albumid 
left join invoice_items i on i.trackid == t.trackid 
left join artists r on a.artistid == r.artistid 
group by r.artistid 
order by 2 desc 
limit 10 ),
with total_album as (
Select 
r.name as artist, 
count(a.artistId) as TotalAlbums from albums a 
left join artists r where a.artistid == r.artistid 
group by a.artistid 
order by 2 desc 
limit 10 )

select artist, TotalTrackSales, TotalAlbums
from total_track_sales ts inner join total_album ta
on ts.artist = ta.artist

Just to merge your 2 queries, you can do the following using CTE:

with total_track_sales as (
Select 
r.name as artist, 
count (i.quantity) as TotalTrackSales 
from albums a 
left join tracks t on t.albumid == a.albumid 
left join invoice_items i on i.trackid == t.trackid 
left join artists r on a.artistid == r.artistid 
group by r.artistid 
order by 2 desc 
limit 10 ),
with total_album as (
Select 
r.name as artist, 
count(a.artistId) as TotalAlbums from albums a 
left join artists r where a.artistid == r.artistid 
group by a.artistid 
order by 2 desc 
limit 10 )

select artist, TotalTrackSales, TotalAlbums
from total_track_sales ts inner join total_album ta
on ts.artist = ta.artist
绿萝 2025-01-17 21:05:35

您可以尝试使用 DISTINCT 并组合聚合和窗口函数进行单个查询。

select * 
from (
   select 
      r.name as artist, 
      count (i.quantity) as TotalTrackSales,
      row_number() over (order by count (i.quantity) desc) rnT,
      count (distinct a.albumid) as totalAlbums,
      row_number() over (order by count (distinct a.albumid) desc) rnA,
   from albums a 
   left join tracks t on t.albumid == a.albumid 
   left join invoice_items i on i.trackid == t.trackid 
   left join artists r on a.artistid == r.artistid 
   group by r.artistid 
)
where rnT <= 10 or rnA <= 10

You can try a single query using DISTINCT and combining aggregates and window functions.

select * 
from (
   select 
      r.name as artist, 
      count (i.quantity) as TotalTrackSales,
      row_number() over (order by count (i.quantity) desc) rnT,
      count (distinct a.albumid) as totalAlbums,
      row_number() over (order by count (distinct a.albumid) desc) rnA,
   from albums a 
   left join tracks t on t.albumid == a.albumid 
   left join invoice_items i on i.trackid == t.trackid 
   left join artists r on a.artistid == r.artistid 
   group by r.artistid 
)
where rnT <= 10 or rnA <= 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文