【问题标题】:Hackerrank Problem:15 days of learning SQL (Stuck at count section in subpart)Hackerrank 问题:学习 SQL 的 15 天(卡在子部分的计数部分)
【发布时间】:2022-08-03 22:37:12
【问题描述】:

我一直在尝试解决以下问题 https://www.hackerrank.com/challenges/15-days-of-learning-sql/problem?isFullScreen=true 但看起来似乎卡在查找在给定开始日期之后的每个日期提交的hacker_ids 的计数。以下是2个版本的解决方案max_submissions如果多个最大日期正确,则给出每个日期的最大提交计数,ID 最低,但在最终查询计数中,我无法获得正确的计数,对于每个hacker_id 每天提交的所有日期,计数为 35 .只有第二列是唯一的黑客在输出中计数,我无法得到我得到 35 作为所有或其他值的计数值,这似乎与预期输出不同但逻辑似乎是正确的

with max_submissions
as 
(
Select t.submission_date,t.hacker_id,t.cnt,h.name From
(Select * from 
(Select submission_date, hacker_id, cnt, dense_rank() over (partition by submission_date order by cnt desc,hacker_id asc) as rn
from
(Select 
submission_date, hacker_id, count(submission_id) cnt
from 
submissions
where submission_date between \'2016-03-01\' and \'2016-03-15\' 
group by submission_date, hacker_id
) 
)where rn =1
) t join 
hackers h on t.hacker_id=h.hacker_id
),
t1
as
(
select  hacker_id
from
(
Select 
 hacker_id, lead(submission_date) over ( order by hacker_id,submission_date)
 -submission_date cnt
from 
submissions
where submission_date between \'2016-03-01\' and \'2016-03-15\' 
order by hacker_id asc, submission_date asc)
group by hacker_id having sum(case when cnt=1 then 1 else 0 end) =14)
select s.submission_date,count( t1.hacker_id)
from submissions s
join 
t1 on
s.hacker_id=t1.hacker_id
group by s.submission_date;
  • edit您的问题整齐地格式化您的代码并解释您的代码。你为什么使用LEADHAVING 子句是干什么用的?
  • 当然,将在一段时间内编辑格式。我使用 Lead 按日期的顺序减去下一个日期,因此,如果我每次都得到 1,这意味着通过 sum 连续计数为 1,我指向那些遵循相同的 id。

标签: sql oracle


【解决方案1】:

这应该会给你正确的结果:

WITH calendar (day) AS (
  -- Generate a calendar so we don't need to assume that there will always be a submission
  -- every day.
  SELECT DATE '2016-03-01' + LEVEL - 1 AS day
  FROM   DUAL
  CONNECT BY LEVEL <= 15
),
daily_hacker_submissions (submission_date, hacker_id, num_submissions) AS (
  -- Find the number of submissions for hackers on each day.
  SELECT c.day,
         hacker_id,
         COUNT(*) AS num_submissions
  FROM   calendar c
         LEFT OUTER JOIN submissions s
         ON (
                -- Don't assume dates are always midnight.
                c.day <= s.submission_date
            AND s.submission_date < c.day + 1
            )
  GROUP BY
         c.day,
         s.hacker_id
),
daily_submissions (submission_date, num_hackers, hacker_id ) AS (
  -- Find the number of hackers on each day and the hacker with the greatest number of
  -- submissions and the least hacker id.
  SELECT submission_date,
         COUNT(DISTINCT hacker_id),
         MIN(hacker_id) KEEP (DENSE_RANK LAST ORDER BY num_submissions)
  FROM   daily_hacker_submissions
  GROUP BY
         submission_date
)
-- Include the hacker's name
SELECT d.submission_date,
       d.num_hackers,
       d.hacker_id,
       h.name
FROM   daily_submissions d
       LEFT OUTER JOIN hackers h
       ON (d.hacker_id = h.hacker_id)

其中,对于样本数据:

CREATE TABLE submissions (submission_date, submission_id, hacker_id, score) AS
SELECT DATE '2016-03-01',  1, 1,  80 FROM DUAL UNION ALL
SELECT DATE '2016-03-01',  2, 1,  90 FROM DUAL UNION ALL
SELECT DATE '2016-03-01',  3, 1, 100 FROM DUAL UNION ALL
SELECT DATE '2016-03-01',  4, 2,  90 FROM DUAL UNION ALL
SELECT DATE '2016-03-01',  5, 2, 100 FROM DUAL UNION ALL
SELECT DATE '2016-03-02',  6, 1, 100 FROM DUAL UNION ALL
SELECT DATE '2016-03-02',  7, 2,  90 FROM DUAL UNION ALL
SELECT DATE '2016-03-02',  8, 2, 100 FROM DUAL UNION ALL
SELECT DATE '2016-03-02',  9, 3,  80 FROM DUAL UNION ALL
SELECT DATE '2016-03-02', 10, 3, 100 FROM DUAL;

CREATE TABLE hackers (hacker_id, name) AS
SELECT 1, 'Alice' FROM DUAL UNION ALL
SELECT 2, 'Betty' FROM DUAL UNION ALL
SELECT 3, 'Carol' FROM DUAL;

输出:

SUBMISSION_DATE NUM_HACKERS HACKER_ID NAME
2016-03-01 00:00:00 2 1 Alice
2016-03-02 00:00:00 3 2 Betty
2016-03-03 00:00:00 0 null null
... ... ... ...

db<>小提琴here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多