pg实现月累计

获取每月累计数据:

​​​


SELECT a.month, SUM(b.total) AS total
FROM (
SELECT  month, SUM(sum) AS total
FROM (
SELECT  to_char(date("Joinin"),'YYYY-MM') AS month
, COUNT(*) AS sum
FROM "APP_HR_Staff_Basic_Info"
GROUP BY month
) ptuser
GROUP BY month
) a
JOIN (
SELECT  month, SUM(sum) AS total
FROM (
SELECT  to_char(date("Joinin"),'YYYY-MM') AS month
, COUNT(*) AS sum
FROM "APP_HR_Staff_Basic_Info"
GROUP BY month
) ptuser
GROUP BY month
) b
ON a.MONTH >= b.MONTH
GROUP BY a.MONTH
ORDER BY a.month;

效果:pg实现月累计_第1张图片

获取每月增长人数:

select t_incre.ym,t_acc.total ,t_incre.incre_num, case when t_incre.incre_num != 0 and t_incre.incre_num != t_acc.total then  t_incre.incre_num/ t_acc.total else 0 end as rate
from (
SELECT a.month, SUM(b.total) AS total
FROM (
    SELECT  month, SUM(sum) AS total
    FROM (
        SELECT to_char(date("Joinin"),'YYYY-MM') AS month
            , COUNT(*) AS sum
        FROM "APP_HR_Staff_Basic_Info"
        GROUP BY month
    ) ptuser
    GROUP BY month
) a
  left join (
        SELECT month, SUM(sum) AS total
        FROM (
            SELECT to_char(date("Joinin"),'YYYY-MM') AS month
                , COUNT(*) AS sum
            FROM "APP_HR_Staff_Basic_Info"
            GROUP BY month
        ) ptuser
        GROUP BY month
    ) b
    ON a.MONTH >= b.MONTH
GROUP BY a.MONTH
ORDER BY a.month) t_acc
left join (select to_char(date("Joinin"),'YYYY-MM') as ym,count(1) as incre_num
from "APP_HR_Staff_Basic_Info"
group by ym
order by ym) t_incre on t_acc.month = t_incre.ym;

 

 

你可能感兴趣的:(postgres,数据库,postgresql,数据库)