如何按周组织此查询

时间:2017-12-14 20:04:06

标签: postgresql

到目前为止,这是我的查询:

select one.week, total, comeback, round(comeback)::Numeric / total::numeric  * 100  as comeback_percent
FROM
    (
        SELECT count(username) as total, week
        FROM
            (
            select row_number () over (partition by u.id order by creation_date) as row, username, date_trunc ('month', creation_date)::date AS week
            FROM users u
            left join entries e on u.id = e.user_id
            where ((entry_type = 0 and distance >= 1) or (entry_type = 1 and seconds_running >= 600))
            ) x
        where row = 1
        group by week
        order by week asc
    ) one

join

    (
        SELECT count(username) as comeback, week
        FROM
            (
            select row_number () over (partition by u.id order by creation_date) as row, username, runs_completed, date_trunc ('month', creation_date)::date AS week
            FROM entries e
            left join users u on e.user_id = u.id
            where ((entry_type = 0 and distance >= 1) or (entry_type = 1 and seconds_running >= 600))
            ) y
        where runs_completed > 1 and row = 1
        group by week
        order by week asc
    ) two 

on one.week = two.week

我想要完成的是,为已完成一次运行的用户返回一个折线图,按周分组,并为已完成第二次运行的任何人分配该周的百分比,而不是仅在该周内。自我们开始以来,我们的渠道已经提高了5倍,但生成的折线图并未显示类似的结果。

我可能错误地将它们连接在一起,或者可能有更简洁的方法来使用CTE或窗口函数来执行此查询,我对所有建议都持开放态度。谢谢!

如果您需要表格或更多信息,请告知我们。我很乐意提供任何可能需要的东西。

0 个答案:

没有答案