趋势 SQL 查询
所以我想做的是制定一个趋势算法,我需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的?aud_songs? (AID 指向的表)添加以下列
在 aud_plays 表中创建一个 AFTER INSERT 触发器来增加 aud_song,请使用 BIGINT。 TotalPlays。
实时计算每个请求的趋势会给您的服务器带来负担,因此最好只需运行一个作业,每 5 分钟更新一次数据。因此,创建一个 SQL 代理作业,每 X 分钟运行一次,更新 Last7dPlays 和 Last24hrPlays。
我还建议从 aud_plays 中删除旧记录(可能早于 7 天,因为您将拥有 TotalPlays 触发器。
应该很容易弄清楚如何计算您的 1 和 2 (从问题中)。这里是3 的 SQL。
注意 我使 T-SQL 非常通用且未经优化,以说明该过程是如何工作的。
In your ?aud_songs? (the one the AID points to) table add the following columns
In your aud_plays table create an AFTER INSERT trigger that will increment aud_song.TotalPlays.
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.
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.
NOTE I made the T-SQL pretty generic and unoptimized to illustrate how the process works.