趋势 SQL 查询

发布于 2025-01-05 05:07:32 字数 631 浏览 1 评论 0原文

所以我想做的是制定一个趋势算法,我需要 SQL 代码方面的帮助,因为我无法让它运行。

该算法包含三个方面:(我对更好的趋势算法的想法完全持开放态度)

1.Plays during 24h / Total plays of the song
2.Plays during 7d / Total plays of the song 
3.Plays during 24h / The value of plays of the most played item over 24h (whatever item leads the play count over 24h)

每个方面的价值均为 0.33,最大值可能为 1.0。

第三个方面是必要的,因为新上传的项目将自动位于顶部位置,除非它们是一种将其下拉的方法。

该表名为 aud_plays,列为:

PlayID: Just an auto-incrementing ID for the table
AID: The id of the song
IP: ip address of the user listening
time: UNIX time code

我尝试了一些 sql 代码,但我无法使其正常工作。

So what I am trying to do is make a trending algorithm, i need help with the SQL code as i cant get it to go.

There are three aspects to the algorithm: (I am completely open to ideas on a better trend algorithm)

1.Plays during 24h / Total plays of the song
2.Plays during 7d / Total plays of the song 
3.Plays during 24h / The value of plays of the most played item over 24h (whatever item leads the play count over 24h)

Each aspect is to be worth 0.33, for a maximum value of 1.0 being possible.

The third aspect is necessary as newly uploaded items would automatically be at top place unless their was a way to drop them down.

The table is called aud_plays and the columns are:

PlayID: Just an auto-incrementing ID for the table
AID: The id of the song
IP: ip address of the user listening
time: UNIX time code

I have tried a few sql codes but im pretty stuck being unable to get this to work.

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

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

发布评论

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

评论(1

勿忘初心 2025-01-12 05:07:32

在你的?aud_songs? (AID 指向的表)添加以下列

  • Last24hrPlays INT - 如果您计划获得十亿以上
  • Last7dPlays INT
  • TotalPlays INT

在 aud_plays 表中创建一个 AFTER INSERT 触发器来增加 aud_song,请使用 BIGINT。 TotalPlays。

UPDATE aud_song SET TotalPlays = TotalPlays + 1 WHERE id = INSERTED.aid

实时计算每个请求的趋势会给您的服务器带来负担,因此最好只需运行一个作业,每 5 分钟更新一次数据。因此,创建一个 SQL 代理作业,每 X 分钟运行一次,更新 Last7dPlays 和 Last24hrPlays。

UPDATE aud_songs SET Last7dPlays = (SELECT COUNT(*) FROM aud_plays WHERE aud_plays.aid = aud_songs.id AND aud_plays.time BETWEEN GetDate()-7 AND GetDate()), 
    Last24hrPlays = (SELECT COUNT(*) FROM aud_plays WHERE aud_plays.aid = aud_songs.id AND aud_plays.time BETWEEN GetDate()-1 AND GetDate())

我还建议从 aud_plays 中删除旧记录(可能早于 7 天,因为您将拥有 TotalPlays 触发器。

应该很容易弄清楚如何计算您的 1 和 2 (从问题中)。这里是3 的 SQL。

SELECT cast(Last24hrPlays as float) / (SELECT MAX(Last24hrPlays) FROM aud_songs) FROM aud_songs WHERE aud_songs.id = @ID

注意 我使 T-SQL 非常通用且未经优化,以说明该过程是如何工作的。

In your ?aud_songs? (the one the AID points to) table add the following columns

  • Last24hrPlays INT -- use BIGINT if you plan on getting billion+
  • Last7dPlays INT
  • TotalPlays INT

In your aud_plays table create an AFTER INSERT trigger that will increment aud_song.TotalPlays.

UPDATE aud_song SET TotalPlays = TotalPlays + 1 WHERE id = INSERTED.aid

Calculating your trending in real time for every request would be taxing on your server, so it's best to just run a job to update the data every ~5 minutes. So create a SQL Agent Job to run every X minutes that updates Last7dPlays and Last24hrPlays.

UPDATE aud_songs SET Last7dPlays = (SELECT COUNT(*) FROM aud_plays WHERE aud_plays.aid = aud_songs.id AND aud_plays.time BETWEEN GetDate()-7 AND GetDate()), 
    Last24hrPlays = (SELECT COUNT(*) FROM aud_plays WHERE aud_plays.aid = aud_songs.id AND aud_plays.time BETWEEN GetDate()-1 AND GetDate())

I would also recommend removing old records from aud_plays (possibly older than 7days since you will have the TotalPlays trigger.

It should be easy to figure out how to calculate your 1 and 2 (from the question). Here's the SQL for 3.

SELECT cast(Last24hrPlays as float) / (SELECT MAX(Last24hrPlays) FROM aud_songs) FROM aud_songs WHERE aud_songs.id = @ID

NOTE I made the T-SQL pretty generic and unoptimized to illustrate how the process works.

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