Sales and Music Charts
L3
PostgresChinook
Create monthly sales dashboard and top music charts system for tracking business performance and trending content.
Created by Lingxiao Du
2025-08-12
Reporting And AnalyticsStatistical AggregationSchema Design
Model Ranking
Click on the dots to view the trajectory of each task run
Task State
Table "Album" {
"AlbumId" int4 [pk, not null]
"Title" varchar(160) [not null]
"ArtistId" int4 [not null]
Indexes {
ArtistId [type: btree, name: "IFK_AlbumArtistId"]
}
}
Table "Artist" {
"ArtistId" int4 [pk, not null]
"Name" varchar(120)
}
Table "Customer" {
"CustomerId" int4 [pk, not null]
"FirstName" varchar(40) [not null]
"LastName" varchar(20) [not null]
"Company" varchar(80)
"Address" varchar(70)
"City" varchar(40)
"State" varchar(40)
"Country" varchar(40)
"PostalCode" varchar(10)
"Phone" varchar(24)
"Fax" varchar(24)
"Email" varchar(60) [not null]
"SupportRepId" int4
Indexes {
SupportRepId [type: btree, name: "IFK_CustomerSupportRepId"]
}
}
Table "Employee" {
"EmployeeId" int4 [pk, not null]
"LastName" varchar(20) [not null]
"FirstName" varchar(20) [not null]
"Title" varchar(30)
"ReportsTo" int4
"BirthDate" timestamp
"HireDate" timestamp
"Address" varchar(70)
"City" varchar(40)
"State" varchar(40)
"Country" varchar(40)
"PostalCode" varchar(10)
"Phone" varchar(24)
"Fax" varchar(24)
"Email" varchar(60)
Indexes {
ReportsTo [type: btree, name: "IFK_EmployeeReportsTo"]
}
}
Table "Genre" {
"GenreId" int4 [pk, not null]
"Name" varchar(120)
}
Table "Invoice" {
"InvoiceId" int4 [pk, not null]
"CustomerId" int4 [not null]
"InvoiceDate" timestamp [not null]
"BillingAddress" varchar(70)
"BillingCity" varchar(40)
"BillingState" varchar(40)
"BillingCountry" varchar(40)
"BillingPostalCode" varchar(10)
"Total" numeric(10,2) [not null]
Indexes {
CustomerId [type: btree, name: "IFK_InvoiceCustomerId"]
}
}
Table "InvoiceLine" {
"InvoiceLineId" int4 [pk, not null]
"InvoiceId" int4 [not null]
"TrackId" int4 [not null]
"UnitPrice" numeric(10,2) [not null]
"Quantity" int4 [not null]
Indexes {
InvoiceId [type: btree, name: "IFK_InvoiceLineInvoiceId"]
TrackId [type: btree, name: "IFK_InvoiceLineTrackId"]
}
}
Table "MediaType" {
"MediaTypeId" int4 [pk, not null]
"Name" varchar(120)
}
Table "Playlist" {
"PlaylistId" int4 [pk, not null]
"Name" varchar(120)
}
Table "PlaylistTrack" {
"PlaylistId" int4 [not null]
"TrackId" int4 [not null]
Indexes {
(PlaylistId, TrackId) [type: btree, name: "PK_PlaylistTrack"]
TrackId [type: btree, name: "IFK_PlaylistTrackTrackId"]
}
}
Table "Track" {
"TrackId" int4 [pk, not null]
"Name" varchar(200) [not null]
"AlbumId" int4
"MediaTypeId" int4 [not null]
"GenreId" int4
"Composer" varchar(220)
"Milliseconds" int4 [not null]
"Bytes" int4
"UnitPrice" numeric(10,2) [not null]
Indexes {
AlbumId [type: btree, name: "IFK_TrackAlbumId"]
GenreId [type: btree, name: "IFK_TrackGenreId"]
MediaTypeId [type: btree, name: "IFK_TrackMediaTypeId"]
}
}
Ref "FK_AlbumArtistId":"Artist"."ArtistId" < "Album"."ArtistId"
Ref "FK_CustomerSupportRepId":"Employee"."EmployeeId" < "Customer"."SupportRepId"
Ref "FK_EmployeeReportsTo":"Employee"."EmployeeId" < "Employee"."ReportsTo"
Ref "FK_InvoiceCustomerId":"Customer"."CustomerId" < "Invoice"."CustomerId"
Ref "FK_InvoiceLineInvoiceId":"Invoice"."InvoiceId" < "InvoiceLine"."InvoiceId"
Ref "FK_InvoiceLineTrackId":"Track"."TrackId" < "InvoiceLine"."TrackId"
Ref "FK_PlaylistTrackPlaylistId":"Playlist"."PlaylistId" < "PlaylistTrack"."PlaylistId"
Ref "FK_PlaylistTrackTrackId":"Track"."TrackId" < "PlaylistTrack"."TrackId"
Ref "FK_TrackAlbumId":"Album"."AlbumId" < "Track"."AlbumId"
Ref "FK_TrackGenreId":"Genre"."GenreId" < "Track"."GenreId"
Ref "FK_TrackMediaTypeId":"MediaType"."MediaTypeId" < "Track"."MediaTypeId"
Instruction
Create a monthly sales dashboard and top music charts system for Chinook's management team to track business performance and identify trending music content.
Your Tasks:
-
Build the monthly sales summary table — create a table called
monthly_sales_summaryin thepublicschema with:year_month(varchar) — format as 'YYYY-MM' (e.g., '2009-01')total_invoices(integer) — number of invoices in that monthtotal_revenue(decimal) — sum of all invoice totals for the monthtotal_tracks_sold(integer) — total quantity of individual tracks soldaverage_invoice_value(decimal) — average invoice amount for the monthunique_customers(integer) — count of distinct customers who made purchases
-
Create the music charts table — build a table called
top_music_chartsin thepublicschema with:chart_type(varchar) — either 'top_tracks', 'top_albums', or 'top_artists'rank_position(integer) — ranking from 1 to 10item_id(integer) — ID of the track, album, or artistitem_name(varchar) — name of the track, album, or artisttotal_revenue(decimal) — total revenue generated by this item
-
Populate the monthly sales data:
- Calculate metrics for each month that has invoice data
- Use invoice date to determine the month
- Note: Each invoice can contain multiple invoice lines (tracks)
-
Generate the top 10 charts:
- Top Tracks: Rank tracks by total quantity sold across all invoices
- Top Albums: Rank albums by total revenue generated from their tracks
- Top Artists: Rank artists by total revenue from all their tracks across all albums
-
Business rules to follow:
- Only include months where at least one invoice exists
- For album rankings, sum revenue from all tracks in each album
- For artist rankings, sum revenue from all tracks across all their albums
- Handle ties by using item name alphabetically as tiebreaker
- Exclude any items with zero sales
This system will provide clear, actionable business intelligence for monthly reporting and music trend analysis.
Verify
Python