【问题标题】:How to calculate percentage over multiple rows over group by如何通过分组计算多行的百分比
【发布时间】:2019-08-09 13:29:50
【问题描述】:

对于我们的一些票的解决率,我想计算解决票相对于同月同类型票总数的百分比 我能够获得已解决的票证数量,但我错过了同一类型和同一月份的所有票证的总数,因此始终为 0% 或 100%。 问题= 我如何累积工单数量,以便计算每月解决工单的百分比、类型、状态?

我按时间分组,输入 AND 状态(请参阅下面的查询)。所以总的状态总是被 group by 否决。 我尝试过:累积、汇总、枢轴、联合

    SELECT date_format(st.createdDate,'%Y%m') createdAt,
       count(st.id) COUNT,
                    st.typeId,
                    st.status,
                    sum(st.status2) status2,
                    sum(st.notStatus2) notStatus2,
                    ((sum(st.status2)/sum(st.status2)+sum(st.notStatus2))*100) AS pct
FROM
  (SELECT t.id,
          t.added createdDate,
          t.type_id typeId,
          tt.name ticketType,
          t.status,
          if(t.status=2,1,0) AS status2,
          if(t.status!=2,1,0) AS notStatus2,
          datediff(closed,added) doorloopTijdDgn
   FROM tickets t
   JOIN ticket_types tt ON tt.id=t.type_id
   WHERE added BETWEEN '2018-07-01' AND '2019-07-01' ) AS st #singletickets
GROUP BY createdAt,
         ticketType,
         status;

对于前两行(类型 = 62),我预计 37 个已解决的票中有 33 个 = 89,2%
所以 pct 的 "sum(st.status2)+sum(st.notStatus2)" 应该是 37

现在我明白了:

createdAt  count  typeId  status  status2 notStatus2  pct
   201807     33      62       2       33          0  100.0000
   201807      4      62       6        0          4  NULL
   201807     31      38       2       31          0  100.0000
   201807      3      38       6        0          3  NULL
   201807     15      12       2       15          0  100.0000
   201807     11      11       2       11          0  100.0000
   201807      1      48       2        1          0  100.0000
   201807     19      30       2       19          0  100.0000
   201807      5       9       2       5           0  100.0000
   201807     12      33       2       12          0  100.0000
   201807      1      52       2        1          0  100.0000
   201807      2      45       2        2          0  100.0000
   201807     41      58       2       41          0  100.0000
   201807      3      58       6        0          3  NULL
   201807      1      41       6        0          1  NULL
   201807      1      13       2        1          0  100.0000
   201807     35      66       2       35          0  100.0000
   201807      8      20       2        8          0  100.0000
   201807      1      20       4        0          1  NULL
   201807      5      10       2        5          0  100.0000
   201807     12      68       2       12          0  100.0000
   201807      1      65       2        1          0  100.0000
   201807     20       7       2       20          0  100.0000
   201807      2      19       2        2          0  100.0000
   201807      4      43       2        4          0  100.0000
   201807      3      34       2        3          0  100.0000
   201807     24      67       2       24          0  100.0000
   201807     43      46       2       43          0  100.0000
   201807      1      46       6        0          1  NULL

我想要什么:

createdAt  count  typeId  status  status2 notStatus2  pct
   201807     33      62       2       33          0  89.2000
   201807      4      62       6        0          4  0
   201807     31      38       2       31          0  93.9000
   201807      3      38       6        0          3  0
   201807     15      12       2       15          0  100.0000
   201807     11      11       2       11          0  100.0000
   201807      1      48       2        1          0  100.0000
   201807     19      30       2       19          0  100.0000
   201807      5       9       2       5           0  100.0000
   201807     12      33       2       12          0  100.0000
   201807      1      52       2        1          0  100.0000
   201807      2      45       2        2          0  100.0000
   201807     41      58       2       41          0  93.2000
   201807      3      58       6        0          3  0
   201807      1      41       6        0          1  0
   201807      1      13       2        1          0  100.0000
   201807     35      66       2       35          0  100.0000
   201807      8      20       2        8          0  88.8000
   201807      1      20       4        0          1  0

我有 mysql 5.7.12 可以使用

【问题讨论】:

  • 您的查询没有多大意义。哪一栏表示票证是否已解决?
  • status=2 = 未解决所有其他状态
  • 添加了我想要从我的查询中获得的示例 MCRE 是我提供的恕我直言,更不用说我遇到的问题
  • 您尚未提供 MCRE。这里没有“R”
  • 好的,我明白了,必须显示虚拟数据集和虚拟查询才能“重现”问题。本周晚些时候再回来,因为我手头还有其他问题。感谢您的建议

标签: mysql sum multiple-columns percentage


【解决方案1】:

看来你错了()

应该是

((sum(st.status2)/(sum(st.status2)+sum(st.notStatus2)))*100) as pct

【讨论】:

  • 将 null 更改为 '0',但不能解决我的总数和百分比问题
猜你喜欢
  • 1970-01-01
  • 2021-01-13
  • 2022-11-21
  • 1970-01-01
  • 2017-11-12
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多