有个搞数仓的朋友不知道从哪儿弄了个题。。。
做了做体验了一下。。。
记录记录。
分析
要保证每天都要做新题
5天必须都做题,不然GG
最后一天必须做新题,如果最后一天做新题了,前面那几天没做新题,做的是老题
最后一天,如果没做题会被踢出
最后一天做的题,不是新题记录0,当求和之后为0,说明没有新题,那就踢出
如果是最后一天做了新题,第四天没做新题
下面的问题需要处理,
用户a,前四天(1,2,3)
第一天:1,2,3
第二天:1,2,3
第三天:1,2,3
第四天:1,2,3
第五天:1,2,3,4文章来源:https://www.toymoban.com/news/detail-600350.html
保证最后一天做新题的案例:文章来源地址https://www.toymoban.com/news/detail-600350.html
SELECT
info.user_id
FROM
(
SELECT
two.user_id,
sum(
IF
(
two.question_id IN ( SELECT DISTINCT one.question_id FROM log_table AS one WHERE one.date between "2022-09-19" and "2022-09-22" AND one.user_id = two.user_id ),
0,
1
)) AS num
FROM
log_table AS two
WHERE
two.date = "2022-09-23"
GROUP BY
two.user_id
) info
WHERE
info.num >0
补充完善
select
*
from (SELECT
info.user_id
FROM
(
SELECT
two.user_id,
sum(
IF
(
two.question_id IN ( SELECT DISTINCT one.question_id FROM log_table AS one WHERE one.date between "2022-09-19" and "2022-09-22" AND one.user_id = two.user_id ),
0,
1
)) AS num
FROM
log_table AS two
WHERE
two.date = "2022-09-23"
GROUP BY
two.user_id
) info
WHERE
info.num >0) as d5
left join (SELECT
info.user_id
FROM
(
SELECT
two.user_id,
sum(
IF
(
two.question_id IN ( SELECT DISTINCT one.question_id FROM log_table AS one WHERE one.date between "2022-09-19" and "2022-09-21" AND one.user_id = two.user_id ),
0,
1
)) AS num
FROM
log_table AS two
WHERE
two.date = "2022-09-22"
GROUP BY
two.user_id
) info
WHERE
info.num >0) as d4
on d5.user_id = d4.user_id
left join (SELECT
info.user_id
FROM
(
SELECT
two.user_id,
sum(
IF
(
two.question_id IN ( SELECT DISTINCT one.question_id FROM log_table AS one WHERE one.date between "2022-09-19" and "2022-09-20" AND one.user_id = two.user_id ),
0,
1
)) AS num
FROM
log_table AS two
WHERE
two.date = "2022-09-21"
GROUP BY
two.user_id
) info
WHERE
info.num >0) as d3
on d5.user_id = d3.user_id
left join (SELECT
info.user_id
FROM
(
SELECT
two.user_id,
sum(
IF
(
two.question_id IN ( SELECT DISTINCT one.question_id FROM log_table AS one WHERE one.date between "2022-09-19" and "2022-09-19" AND one.user_id = two.user_id ),
0,
1
)) AS num
FROM
log_table AS two
WHERE
two.date = "2022-09-20"
GROUP BY
two.user_id
) info
WHERE
info.num >0) as d2
on d5.user_id = d2.user_id
left join (SELECT
info.user_id
FROM
(
SELECT
two.user_id,
sum(
IF
(
two.question_id IN ( SELECT DISTINCT one.question_id FROM log_table AS one WHERE one.date between "2022-09-18" and "2022-09-18" AND one.user_id = two.user_id ),
0,
1
)) AS num
FROM
log_table AS two
WHERE
two.date = "2022-09-19"
GROUP BY
two.user_id
) info
WHERE
info.num >0) as d1
on d5.user_id = d1.user_id
到了这里,关于一道SQL题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!