Codewars上一道5 kyu的SQL题

具体思路和过程以后有时间再来整理(Flag)。

原题目的url是https://www.codewars.com/kata/calculating-running-total

参考了一下简单的情况 http://sql55.com/query/cumulative-amount-1.php ,然后自己建表鼓捣了一会儿,

终于得(meng)出了一个可行的答案:

SELECT CAST(p1.created_at AS DATE) AS date,
       COUNT(DISTINCT p1.title) AS count,
       COUNT(p2.title)/COUNT(DISTINCT p1.title) AS total
FROM posts p1 INNER JOIN posts p2
  ON to_char(p1.created_at, 'YYYY-MM-DD') >= to_char(p2.created_at, 'YYYY-MM-DD')
GROUP BY date
ORDER BY date

Contents


本站采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议

知识共享许可协议