繁体   English   中英

MySQL-比较同一表中的行与日期范围

[英]MySQL - comparing rows in the same table with date ranges

我对MySQL查询有一点问题。 我需要比较同一张表中的行。 这是我的表格“ video_stats”:

----------------------------------
video       | date       | views
----------------------------------
8kCge8vWnIg | 2017-05-15 | 4
8kCge8vWnIg | 2017-05-16 | 20
8kCge8vWnIg | 2017-05-17 | 11
8kCge8vWnIg | 2017-05-18 | 12
rYyiTP0srNs | 2017-05-15 | 627
rYyiTP0srNs | 2017-05-16 | 6414
rYyiTP0srNs | 2017-05-17 | 21076
rYyiTP0srNs | 2017-05-18 | 34434
cRfEo-ZzU4U | 2017-05-17 | 17049
cRfEo-ZzU4U | 2017-05-18 | 12466

MySQL查询:

SELECT t1.video, 
       t1.date, 
       t1.views,
       t1c.video,
       t1c.date,
       t1c.views
FROM video_stats t1
LEFT JOIN video_stats t1c ON t1.video=t1c.video
           AND t1c.date<='2017-05-16' AND t1c.date>='2017-05-15'
WHERE t1.date>='2017-05-17'
      AND t1.date<='2017-05-18'

结果在这里...

     --------------------------------------------------------------------------
     video       | t1.date    | t1.views | video       | t1c.date   | t1c.date
     --------------------------------------------------------------------------
 #1| 8kCge8vWnIg | 2017-05-17 | 12       | 8kCge8vWnIg | 2017-05-15 | 4
 #2| 8kCge8vWnIg | 2017-05-17 | 12       | 8kCge8vWnIg | 2017-05-16 | 20   
 #3| 8kCge8vWnIg | 2017-05-18 | 11       | 8kCge8vWnIg | 2017-05-15 | 4
 #4| 8kCge8vWnIg | 2017-05-18 | 11       | 8kCge8vWnIg | 2017-05-16 | 20
 #5| rYyiTP0srNs | 2017-05-17 | 21076    | rYyiTP0srNs | 2017-05-15 | 627
 #6| rYyiTP0srNs | 2017-05-17 | 21076    | rYyiTP0srNs | 2017-05-16 | 6414
 #7| rYyiTP0srNs | 2017-05-18 | 34434    | rYyiTP0srNs | 2017-05-15 | 627
 #8| rYyiTP0srNs | 2017-05-18 | 34434    | rYyiTP0srNs | 2017-05-16 | 6414
 #9| cRfEo-ZzU4U | 2017-05-17 | 17049    | NULL        | NULL       | NULL
#10| cRfEo-ZzU4U | 2017-05-18 | 12466    | NULL        | NULL       | NULL

t1.date和t1c.date中的单个日期都没有问题。 但是,如果有多个日期,问题就开始了。 如何排除重复的行:#2和#3,#6和#7? 以及如何正确地将t1.views和t1c.views相加?

最后,我需要这样的东西……但是我不知道怎么做。

video        | t1.views *05/17 + 05/18* | t1c.views *05/15 + 05/16*
-------------------------------------------------------------------
8kCge8vWnIg  | 23                       | 24
rYyiTP0srNs  | 55510                    | 7041
cRfEo-ZzU4U  | 29515                    | NULL

棘手,但有效:

select 
  r1.video,
  r1.views,
  r2.views
from
    (SELECT 
       t1.video, 
       sum(t1.views) as views
    FROM 
       test t1
    WHERE
       t1.date>='2017-05-17'
       AND t1.date<='2017-05-18'
       group by t1.video) r1
   left outer join
    (SELECT 
       t2.video, 
       sum(t2.views) as views
    FROM 
       test t2
    WHERE
       t2.date>='2017-05-15'
       AND t2.date<='2017-05-16' 
       group by t2.video) r2
    on r1.video = r2.video;  
select a.Video,a.views as 'view *05/15 + 05/16*',b.views as 'iew *05/17 + 05/18*' from
(select Video,sum(views) as views from
(SELECT * FROM YOURTABLE) as a where date(`date`)>='2017-05-17' and date(`date`)<='2017-05-18'
GROUP BY Video) as a

left join

(select Video,sum(views) as views from
(SELECT * FROM YOURTABLE) as a where date(`date`)>='2017-05-15' and date(`date`)<='2017-05-16'
GROUP BY Video) as b
on a.Video = b.Video

测试结果:

8kCge8vWnIg 23      24
cRfEo-ZzU4U 29515   
rYyiTP0srNs 55510   7041

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM