累积总和与SQLite中的条件

发布于 2025-02-13 08:14:38 字数 1935 浏览 1 评论 0原文

我有一个看起来像下面的数据集,我需要知道哪些歌曲是最顺序播放的(我正在使用SQLite)。

SELECT endtime, trackname FROM streaming_history AS sh
JOIN albums AS a ON a.albumid = sh.albumid
JOIN tracks AS t ON t.trackid = sh.trackid
末日踪迹名称
2022-03-21 15:48:00Walk
2022-03-21 15:50:00Walk
2022-03-21 15:54:00最后一口气
2022-03-21 15:57:00突破
2022-- 03-21 16:02:00外部
2022-03-21 16:07:00起义
2022-03-21 16:12:00步行
2022-03-21 16:16:00最后一口气
2022-03-21 16:20:00突破
2022-03-21 16:36:00外部
2022-03-22 003-22 00:10:001022-03--03--
: 22 10:53:00在外面

,因为我不得不知道哪些歌曲最多,而是播放了哪首歌,这是最简单的,与Group by to note by by by by by by by by by by by by by by by by by by by。从上面的数据提取物中,外部将以三次(而不是四次)排名第一,第二名是两次。

我设法创建了另一列的标记,上一首播放的歌曲是否与带有滞后和案例的当前行歌曲相同,但似乎无法弄清楚如何使用新列的最后一列的最后一个行信息。

I have a dataset that looks like the one below and I need to know which songs were played in sequence the most (I am using SQLite).

SELECT endtime, trackname FROM streaming_history AS sh
JOIN albums AS a ON a.albumid = sh.albumid
JOIN tracks AS t ON t.trackid = sh.trackid
endtimetrackname
2022-03-21 15:48:00Walk
2022-03-21 15:50:00Walk
2022-03-21 15:54:00One Last Breath
2022-03-21 15:57:00Breakout
2022-03-21 16:02:00Outside
2022-03-21 16:07:00Uprising
2022-03-21 16:12:00Walk
2022-03-21 16:16:00One Last Breath
2022-03-21 16:20:00Breakout
2022-03-21 16:36:00Outside
2022-03-22 00:10:00Outside
2022-03-22 10:53:00Outside

Since I have to know not which songs were played the most, but which song were played the most in sequence, a simple count with group by won't do. From the data extract above, Outside would be first place with three times (not four) and Walk second place with two times.

I managed to create another column flagging whether or not the last played song was the same as the current row song with LAG and CASE, but can't seem to be able to figure out how to create a column to sum using the new column's last row info.

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

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

发布评论

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

评论(1

笨死的猪 2025-02-20 08:14:38

使用sum()窗口函数以获取歌曲序列,通过将您使用lag() and gentregate创建的标志求和。
然后使用rank()窗口函数获取顶级排序歌曲:

WITH cte AS (
  SELECT trackid, count
  FROM (
    SELECT MAX(trackid) trackid, 
           COUNT(*) count,
           RANK() OVER (ORDER BY COUNT(*) DESC) rnk
    FROM (
      SELECT *, SUM(flag) OVER (ORDER BY endtime) grp
      FROM (
        SELECT *, trackid <> LAG(trackid, 1, 0) OVER (ORDER BY endtime) flag
        FROM streaming_history
      )
    )
    GROUP BY grp
  )
  WHERE rnk = 1
)
SELECT t.trackname, c.count
FROM tracks t INNER JOIN cte c
ON c.trackid = t.trackid;

请参阅 demo

Use SUM() window function to get the groups of sequences of songs, by summing the flag that you created with LAG() and aggregate.
Then use RANK() window function to get the top sequenced song(s):

WITH cte AS (
  SELECT trackid, count
  FROM (
    SELECT MAX(trackid) trackid, 
           COUNT(*) count,
           RANK() OVER (ORDER BY COUNT(*) DESC) rnk
    FROM (
      SELECT *, SUM(flag) OVER (ORDER BY endtime) grp
      FROM (
        SELECT *, trackid <> LAG(trackid, 1, 0) OVER (ORDER BY endtime) flag
        FROM streaming_history
      )
    )
    GROUP BY grp
  )
  WHERE rnk = 1
)
SELECT t.trackname, c.count
FROM tracks t INNER JOIN cte c
ON c.trackid = t.trackid;

See the demo.

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