牛客网SQL160

国庆期间每类视频点赞量和转发量_牛客题霸_牛客网

sql 复制代码
select *
from
(
select tag,dt,
sum(单日点赞量)over(partition by tag order by dt rows between 6 preceding and 0 following),
max(单日转发量)over(partition by tag order by dt rows between 6 preceding and 0 following)
from
(
select dt,tag,sum(if_like) 单日点赞量,sum(if_retweet) 单日转发量
from
(
    select id,video_id,if_like,if_retweet,
    date_format(start_time,'%Y-%m-%d') dt
    from tb_user_video_log
    where date_format(start_time,'%Y-%m-%d') between '2021-09-25' and '2021-10-03'
) a
join
(
    select video_id,tag
    from tb_video_info
) b
on a.video_id=b.video_id
group by dt,tag
) c
) d
where dt between '2021-10-01' and '2021-10-03'
order by tag desc,dt

注意点:

  1. date_format中%Y和%y好像不一样 ;

  2. 注意时间相关的函数,如果想当成数字取用要注意是不是带"-"的,小细节问题,如果带"-",你和数字直接去比较肯定不一样。

比较的时候一般用between and比较多

  1. 认识了mysql中三个时间相关的函数:

convert(start_time,char(10))

date_format(start_time,'%Y-%m-%d')

date(start_time)

结果都是2021-09-25的格式

相关推荐
漂着的圆木5 天前
Agent 功能参与度:Copilot 怎么算
sql·数据分析·agent·githubcopilot·度量
旺仔不是程序员5 天前
LIMIT 1:PostgreSQL 只取一行的高效查询姿势
数据库·后端·sql
知识的搬运工旺仔6 天前
唯一索引与 NULL 值:PostgreSQL 主键约束与 NULLS NOT DISTINCT
数据库·后端·sql·postgresql
想念是会呼吸的鱼6 天前
【ClickHouse 常用 SQL 语句整理】
sql·clickhouse
想念是会呼吸的鱼6 天前
MySQL 常用语法整理
sql·mysql
SelectDB技术团队6 天前
StarRocks 迁移至 Apache Doris 完整指南:三步完成结构、数据与业务平滑切换
数据库·人工智能·sql·clickhouse·apache doris·selectdb·湖仓架构升级
imDwAaY6 天前
MySQL MVCC 详解:原理、版本链、Read View 与可见性判断
数据库·sql·mysql
bbq粉刷匠6 天前
触发器(下):事务与锁、binlog 一致性与替代方案
sql