`near '(': 语法错误` 创建每个流派中畅销专辑的视图以及销售情况

发布于 2025-01-10 20:28:06 字数 749 浏览 0 评论 0原文

此代码使用 Chinook 数据库为每个流派中的畅销专辑创建一个名为 BestSeller 的视图,并提供销售额(基于已售曲目的数量),其中包含流派、专辑、艺术家和销售额列。

CREATE VIEW vBestSellingGenreAlbum AS SELECT DISTINCT 
   g.Name Genre, 
   FIRST_VALUE(a.Title) OVER (PARTITION BY g.GenreId ORDER BY COUNT(*) DESC) Album, 
   FIRST_VALUE(r.Name) OVER (PARTITION BY g.GenreId ORDER BY COUNT(*) DESC) Artist, 
   MAX(COUNT(*)) OVER (PARTITION BY g.GenreId) Sales
FROM genres g
INNER JOIN tracks t ON t.GenreId = g.GenreId
INNER JOIN albums a ON a.AlbumId = t.AlbumId
INNER JOIN artists r ON r.ArtistId = a.ArtistId
INNER JOIN invoice_items i ON i.TrackId = t.TrackId
GROUP BY g.GenreId, a.AlbumId;
SELECT * FROM vBestSellingGenreAlbum;

我收到错误

near '(': syntax error

This code uses the Chinook database to create a view called BestSeller for the bestselling album in each genre with sales (based on the quantity of tracks sold) with the columns Genre, Album, Artist, and Sales.

CREATE VIEW vBestSellingGenreAlbum AS SELECT DISTINCT 
   g.Name Genre, 
   FIRST_VALUE(a.Title) OVER (PARTITION BY g.GenreId ORDER BY COUNT(*) DESC) Album, 
   FIRST_VALUE(r.Name) OVER (PARTITION BY g.GenreId ORDER BY COUNT(*) DESC) Artist, 
   MAX(COUNT(*)) OVER (PARTITION BY g.GenreId) Sales
FROM genres g
INNER JOIN tracks t ON t.GenreId = g.GenreId
INNER JOIN albums a ON a.AlbumId = t.AlbumId
INNER JOIN artists r ON r.ArtistId = a.ArtistId
INNER JOIN invoice_items i ON i.TrackId = t.TrackId
GROUP BY g.GenreId, a.AlbumId;
SELECT * FROM vBestSellingGenreAlbum;

I get the error

near '(': syntax error

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

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

发布评论

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

评论(2

等数载,海棠开 2025-01-17 20:28:06

对于 3.25.0 之前的 SQLite 版本,您可以使用 2 级聚合并利用 SQLite 的 裸列

SELECT Genre, Album, Artist, MAX(Sales) Sales
FROM (
  SELECT g.Name Genre, 
         a.Title Album, 
         r.Name Artist, 
         COUNT(*) Sales
  FROM genres g
  INNER JOIN tracks t ON t.GenreId = g.GenreId
  INNER JOIN albums a ON a.AlbumId = t.AlbumId
  INNER JOIN artists r ON r.ArtistId = a.ArtistId
  INNER JOIN invoice_items i ON i.TrackId = t.TrackId
  GROUP BY g.GenreId, a.AlbumId
)
GROUP BY Genre;

请参阅演示

For versions of SQLite prior to 3.25.0, you can do it with 2 levels of aggregation and by utilizing SQLite's feature of bare columns:

SELECT Genre, Album, Artist, MAX(Sales) Sales
FROM (
  SELECT g.Name Genre, 
         a.Title Album, 
         r.Name Artist, 
         COUNT(*) Sales
  FROM genres g
  INNER JOIN tracks t ON t.GenreId = g.GenreId
  INNER JOIN albums a ON a.AlbumId = t.AlbumId
  INNER JOIN artists r ON r.ArtistId = a.ArtistId
  INNER JOIN invoice_items i ON i.TrackId = t.TrackId
  GROUP BY g.GenreId, a.AlbumId
)
GROUP BY Genre;

See the demo.

感受沵的脚步 2025-01-17 20:28:06

如果您使用的是 Linux,请检查 LD_LIBRARY_PATH 中的 sqlite lib > > 3.25。
您的系统中可能安装了多个版本的 sqlite。确保
最新版本(>3.25)在 LD_LIBRARY_PATH 中具有优先权。

If you are using Linux, check your LD_LIBRARY_PATH for sqlite lib > 3.25.
More than one version of sqlite may be installed in your system. Ensure
that the recent version ( >3.25) has precedence in LD_LIBRARY_PATH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文