我需要找到从发行日期起的前60天内每个标题的总分钟流:
xx表:
title_id整数
minutes_streamingfloat
streamstartdatedate (yyyymmdd hh:ss)
yy表:
类型varchar
launch_datedate (yyyymmdd hh:ss)
SELECT xx.title_id, yy.genre, SUM(xx.minutes_streamed) AS total_minutes
FROM xx
JOIN yy
ON xx.titleid = yy.titleid
WHERE xx.streamstartdate BETWEEN yy.launch_date + 60
AND yy.launch_date
GROUP BY 1, 2
这是正确的方法吗?还有其他更"优雅"的方法吗?
你的方法是好的,但是这个逻辑是关闭的:
WHERE xx.streamstartdate between yy.launch_date+60 and yy.launch_date
between的界限必须是和。所以:
WHERE xx.streamstartdate between yy.laucn_date and yy.launch_date+60