一种滚动求和的方法

r8uurelv  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(461)

我有下面的数据集。下面是1993年的记录。这个 Tgrowth 列为 start - end . Started 是在特定月份加入的员工人数,以及 ended 是当月离职的员工人数。

  1. SELECT
  2. r.Tgrowth,
  3. CASE
  4. WHEN t.mon_num = 1 THEN 'JAN'
  5. WHEN t.mon_num = 2 THEN 'FEB'
  6. WHEN t.mon_num = 3 THEN 'MAR'
  7. WHEN t.mon_num = 4 THEN 'APR'
  8. WHEN t.mon_num = 5 THEN 'MAY'
  9. WHEN t.mon_num = 6 THEN 'JUN'
  10. WHEN t.mon_num = 7 THEN 'JUL'
  11. WHEN t.mon_num = 8 THEN 'AUG'
  12. WHEN t.mon_num = 9 THEN 'SEP'
  13. WHEN t.mon_num = 10 THEN 'OCT'
  14. WHEN t.mon_num = 11 THEN 'NOV'
  15. WHEN t.mon_num = 12 THEN 'DEC'
  16. END AS myMONTH
  17. FROM
  18. (SELECT 1 mon_num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) t
  19. LEFT JOIN Reports r ON t.mon_num = r.theMONTH
  20. AND r.Tyear = 1993
  21. GROUP BY r.Tgrowth , myMONTH
  22. ORDER BY t.mon_num ASC

上述结果集如下:,

  1. Tgrowth Month
  2. 1 JAN
  3. 0 FEB
  4. 2 MAR
  5. 0 APR
  6. 0 MAY
  7. 0 JUN
  8. 0 JUL
  9. 0 AUG
  10. 0 SEP
  11. 0 OCT
  12. 0 NOV
  13. 0 DEC

相反,我希望结果显示一个滚动总和,即添加到 Tgrowth 现场。像下面这样,

  1. growth Emp_Count myMONTH
  2. 1 1 JAN
  3. 0 1 FEB
  4. 2 3 MAR
  5. 0 3 APR
  6. 0 3 MAY
  7. 0 3 JUN
  8. 0 3 JUL
  9. 0 3 AUG
  10. 0 3 SEP
  11. 0 3 OCT
  12. 0 3 NOV
  13. 0 3 DEC
hgb9j2n6

hgb9j2n61#

有两种选择:
使用联接
使用变量
使用join的方法如下:

  1. SELECT
  2. t1.Tgrowth,
  3. sum(t2.Tgrowth) as Emp_Count,
  4. CASE
  5. WHEN t1.Month = 1 THEN 'JAN'
  6. WHEN t1.Month = 2 THEN 'FEB'
  7. WHEN t1.Month = 3 THEN 'MAR'
  8. WHEN t1.Month = 4 THEN 'APR'
  9. WHEN t1.Month = 5 THEN 'MAY'
  10. WHEN t1.Month = 6 THEN 'JUN'
  11. WHEN t1.Month = 7 THEN 'JUL'
  12. WHEN t1.Month = 8 THEN 'AUG'
  13. WHEN t1.Month = 9 THEN 'SEP'
  14. WHEN t1.Month = 10 THEN 'OCT'
  15. WHEN t1.Month = 11 THEN 'NOV'
  16. WHEN t1.Month = 12 THEN 'DEC'
  17. END AS myMONTH
  18. FROM (
  19. SELECT
  20. case
  21. when r.growth is not null then r.growth
  22. when r.growth is null then 0
  23. END as Tgrowth,
  24. t.mon_num AS Month
  25. FROM
  26. (SELECT 1 mon_num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
  27. UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
  28. UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) t
  29. LEFT JOIN Reports r ON t.mon_num = r.themonth
  30. AND r.theYear = 1993
  31. GROUP BY r.growth , Month
  32. ORDER BY t.mon_num ASC
  33. ) as t1 join (
  34. SELECT
  35. case
  36. when r.growth is not null then r.growth
  37. when r.growth is null then 0
  38. END as Tgrowth,
  39. t.mon_num AS Month
  40. FROM
  41. (SELECT 1 mon_num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
  42. UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
  43. UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) t
  44. LEFT JOIN Reports r ON t.mon_num = r.themonth
  45. AND r.theYear = 1993
  46. GROUP BY r.growth , Month
  47. ORDER BY t.mon_num ASC
  48. ) as t2 on t1.Month >= t2.Month group by t1.Month;

使用变量解决方案如下:

  1. SET @num := 0;
  2. select
  3. Tgrowth,
  4. @num := @num + Tgrowth as Emp_Count,
  5. CASE
  6. WHEN t1.Month = 1 THEN 'JAN'
  7. WHEN t1.Month = 2 THEN 'FEB'
  8. WHEN t1.Month = 3 THEN 'MAR'
  9. WHEN t1.Month = 4 THEN 'APR'
  10. WHEN t1.Month = 5 THEN 'MAY'
  11. WHEN t1.Month = 6 THEN 'JUN'
  12. WHEN t1.Month = 7 THEN 'JUL'
  13. WHEN t1.Month = 8 THEN 'AUG'
  14. WHEN t1.Month = 9 THEN 'SEP'
  15. WHEN t1.Month = 10 THEN 'OCT'
  16. WHEN t1.Month = 11 THEN 'NOV'
  17. WHEN t1.Month = 12 THEN 'DEC'
  18. END AS myMONTH
  19. from (
  20. SELECT
  21. case
  22. when r.growth is not null then r.growth
  23. when r.growth is null then 0
  24. END as Tgrowth,
  25. t.mon_num AS Month
  26. FROM
  27. (SELECT 1 mon_num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
  28. UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
  29. UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) t
  30. LEFT JOIN Reports r ON t.mon_num = r.themonth
  31. AND r.theYear = 1993
  32. GROUP BY r.growth , Month
  33. ORDER BY t.mon_num ASC ) t1;
展开查看全部
rqcrx0a6

rqcrx0a62#

因为您运行的是mysql 8.0,所以我建议使用递归查询来生成日期,然后使用窗口函数和聚合。
如果你想要整个1993年:

  1. with dates as (
  2. select '1993-01-01' dt
  3. union all
  4. select dt + interval 1 month from dates where dt < '1993-12-01'
  5. )
  6. select
  7. date_format(d.dt, '%b') mymonth,
  8. coalesce(sum(started), 0) - coalesce(sum(ended), 0) growth,
  9. sum(coalesce(sum(started), 0) - coalesce(sum(ended), 0)) over(order by d.dt) emp_count
  10. from dates d
  11. left join reports r on r.theDate >= d.dt and r.theDate < d.dt + interval 1 month
  12. group by d.dt
  13. order by d.dt

这是假设 theDate 存储为 date 数据类型而不是字符串(否则,您需要首先使用 str_to_date() ).
这还考虑到表可能包含给定月份的多行。如果不是这样,那么就不需要聚合:

  1. with dates as (
  2. select '1993-01-01' dt
  3. union all
  4. select dt + interval 1 month from dates where dt < '1993-12-01'
  5. )
  6. select
  7. date_format(d.dt, '%b') mymonth,
  8. coalesce(started, 0) - coalesce(ended, 0) growth,
  9. sum(coalesce(started, 0) - coalesce(ended, 0)) over(order by d.dt) emp_count
  10. from dates d
  11. left join reports r on r.theDate >= d.dt and r.theDate < d.dt + interval 1 month
  12. order by d.dt
展开查看全部

相关问题