Niuke.com's SQL non-technical quick start (6)--time, date

knowledge points

(1) Get system time

1

2

3

4

5

6

7

8

# Get the date and time of the current system

SELECT NOW(); # 2021-12-22 13:50:58

# Get the current system date

SELECT CURDATE(); # 2021-12-22

# Get the current system time

SELECT CURTIME(); # 13:53:11

(2) Get the date of a certain year, month and day

1

2

4

7

year/month函数:year(date)=2021 and month(date)=8

date_format函数:date_format(date, "%Y-%m")="202108"

date like '%2021-08%'

Example: question_practice_detail

id device_id question_id result date
1 2138 111 wrong 2021-05-03
2 3214 112 wrong 2021-05-09
3 3214 113 wrong 2021-06-15
4 6543 111 right 2021-08-13
5 2315 115 right 2021-08-13
6 2315 116 right 2021-08-14
7 2315 117 wrong 2021-08-15
……

28. The operator wants to calculate the number of daily user practice questions in August 2021 , please take out the corresponding data.

方法1:
select day(date) as day,
       count(question_id) as question_cnt
from question_practice_detail
where month(date)=8 and year(date)=2021
group by date

方法2:
select day(date) as day,
       count(question_id) as question_cnt
from question_practice_detail
where date like '%2021-08%'
group by date      ----date换成day也可以


方法3:
select day(date) as day,
       count(question_id) as question_cnt
from question_practice_detail
where date_format(date, "%Y-%m") like "2021-08"
group by date

Based on the example, your query should return the following results:

day question_cnt
13 5
14 2
15 3
16 1
18 1

29. Now the operation wants to check the average probability that the user will come back to the next day after reading the questions on a certain day. Please take out the corresponding data.

Problem-solving ideas:

The retention rate of the next day = the number of all entries in the deduplicated data table / the number of entries in the deduplicated data table that meet the next day’s retention​

SELECT
    COUNT(q2.device_id) / COUNT(q1.device_id) AS avg_ret
FROM
    (SELECT DISTINCT device_id, date FROM question_practice_detail)as q1
LEFT JOIN
    (SELECT DISTINCT device_id, date FROM question_practice_detail) AS q2
ON q1.device_id = q2.device_id AND q2.date = DATE_ADD(q1.date, interval 1 day)

--具体而言,使用两个子查询,查询出两个去重的数据表,
--并使用条件(q2.date应该是q1.date的后一天)进行筛选,
--如下所示(数据未显示完全,从左至右顺序,列表名为 q1.device_id, q1.date, q2.device_id, q2.date)

--注意,MySQL中 COUNT在对列进行计数时不统计值为 null的条目

Problem-solving ideas:

  • Restrictions: Come back the next day.

    Solution 1: The data in the table can be regarded as all the questions that came on the first day, so we need to construct the fields that came on the second day, so we can consider using left join to put together the fields that came on the next day. For those who come the next day, you can use date_add(date1, interval 1 day)=date2 to filter, and use device_id to limit to the same user.

    解法2:用lead函数将同一用户连续两天的记录拼接起来。先按用户分组partition by  device_id,再按日期升序排序order by date,再两两拼接(最后一个默认和null拼 接),即lead(date) over (partition by device_id order by date)

    平均概率:

    解法1:可以count(date1)得到左表全部的date记录数作为分母,count(date2)得到右表关联上了的date记录数作为分子,相除即可得到平均概率

    解法2:检查date2和date1的日期差是不是为1,是则为1(次日留存了),否则为0(次日未留存),取avg即可得平均概率。

--方法1:
select count(date2) / count(date1) as avg_ret
from (
    select
        distinct a.device_id,
        a.date as date1,
        b.date as date2
    from question_practice_detail as a
    left join(
        select distinct device_id, date
        from question_practice_detail
    ) as b
    on a.device_id = b.device_id
        and date_add(a.date, interval 1 day)=b.date
) as id_last_next_date


--方法2:
select avg(if(datediff(date2, date1)=1, 1, 0)) as avg_ret
from (
    select
        distinct device_id,
        date as date1,
        lead(date) over (partition by device_id order by date) as date2
    from (
        select distinct device_id, date
        from question_practice_detail
    ) as uniq_id_date
) as id_last_next_date

返回以下结果:

avg_ret
0.3000

Guess you like

Origin blog.csdn.net/weixin_48272780/article/details/127942072