简体   繁体   中英

aggregating using group by with substraction SQL

DB has a table events storing likes for blog-posts with verb=liked, if a user dislikes the post, it is stored with verb=likeDeleted

**`events`
PostID | Verb | timestamp_gmt**
P121   | liked | 2020-01-05 14:15:23
P157   | liked | 2020-02-07 11:14:12
P121   |likeDeleted| 2020-02-07 11:14:14

Query A:

SELECT 
    YEARWEEK(timestamp_gmt), COUNT(*)
FROM
    `events`
WHERE
    timestamp_gmt > '2020-01-01 00:00:00'
        AND (verb = 'liked')
GROUP BY YEARWEEK(timestamp_gmt)

Query B:

SELECT 
(SELECT 
        COUNT(*)
    FROM
        `events`
    WHERE
        timestamp_gmt > '2020-01-01 00:00:00'
            AND (verb = 'liked')) - (SELECT 
        COUNT(*)
    FROM
        `events`
    WHERE
        timestamp_gmt > '2020-01-01 00:00:00'
            AND (verb = 'likeDeleted')) AS difference

with Query A, you get count of likes grouped by week. with Query B, you get net count of likes and deletedLikes after 1 Jan 2020.

The requirement is to have net count grouped by week.

Happy minor brain exercise for the experts:)

You can try using conditional aggregation- with case expression

select YEARWEEK(timestamp_gmt),count(case when verb='liked' then 1 end)-
       count(case when verb='likeDeleted' then 1 end)
from tablename
WHERE timestamp_gmt>"2020-01-01 00:00:00"
group by YEARWEEK(timestamp_gmt)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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