gpt4 book ai didi

mysql - 15天的Sql From hackerrank

转载 作者:行者123 更新时间:2023-11-30 21:53:18 26 4
gpt4 key购买 nike

我无法理解代码中这一行的用法,有人可以解释一下这个问题或提供一些不同的方法来解决这个问题

问题链接:https://www.hackerrank.com/challenges/15-days-of-learning-sql

代码:

select 
submission_date ,
( SELECT COUNT(distinct hacker_id)
FROM Submissions s2
WHERE s2.submission_date = s1.submission_date
AND ( SELECT COUNT(distinct s3.submission_date)
FROM Submissions s3
WHERE
s3.hacker_id = s2.hacker_id
AND s3.submission_date < s1.submission_date
) = dateDIFF(s1.submission_date , '2016-03-01'))
, ( select hacker_id
from submissions s2
where s2.submission_date = s1.submission_date
group by hacker_id
order by count(submission_id) desc , hacker_id limit 1
) as shit
, ( select name
from hackers where hacker_id = shit
)
FROM
( select distinct submission_date
from submissions) s1
group by submission_date

无法理解为什么他们使用这部分代码中的这一行:

 (s3.submission_date < s1.submission_date) = dateDIFF(s1.submission_date , '2016-03-01')) 

最佳答案

CREATE TABLE #max_submissions (
submission_date date,
hacker_id integer,
submission_count integer,
ordering_row integer
)

insert into #max_submissions
select
submission_date,
hacker_id,
submission_count,
row_number() over(partition by submission_date order by submission_count desc, hacker_id) as ordering_row
from (
select submission_date,
hacker_id,
count(hacker_id) as submission_count
from submissions
group by submission_date, hacker_id
) tbl_submission_count

CREATE TABLE #hacker_counts (
submission_date date,
hacker_count integer
)

insert into #hacker_counts
select tbl.submission_date,
COUNT(distinct tbl.hacker_id) as cc
from (
select *,
(case when (
(select count(*)
from (select distinct *
from (select s1.hacker_id,
s1.submission_date
from Submissions s1
where s1.hacker_id = s.hacker_id and
(s1.submission_date >= '2016-03-01' and
s1.submission_date <= s.submission_date)) t1
) t2
) >= (DATEDIFF(day, '2016-03-01', s.submission_date) + 1) )
then 1
else 0
end) as logic
from Submissions s
) tbl
where tbl.logic = 1
group by tbl.submission_date



select max_submissions.submission_date,
hacker_counts.hacker_count,
max_submissions.hacker_id,
h.name
from #max_submissions max_submissions
inner join hackers h on max_submissions.hacker_id = h.hacker_id
left join #hacker_counts hacker_counts on max_submissions.submission_date = hacker_counts.submission_date
where max_submissions.ordering_row = 1
order by max_submissions.submission_date

drop table #max_submissions
drop table #hacker_counts

关于mysql - 15天的Sql From hackerrank,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46235897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com