更新时间:2023-02-05 08:06:35
我认为您唯一的问题是ELSE 0
.您只需要NULL
值,因为它们将被忽略:
I think your only problem is the ELSE 0
. You simply want NULL
values, because they will be ignored:
SELECT mid as mID,
round((x.qty_sum / x.qty_count), 5) as qtAVG,
round(x.qty_stddev, 5) as qtSTDDEV,
x.qty_count as qtCOUNT,
round((x.rel_sum / x.rel_count), 5) as relAVG,
round(x.rel_stddev, 5) as relSTDDEV,
x.rel_count as relCOUNT,
FROM (SELECT mid,
SUM( mt = 'qt' ) as qty_count,
SUM(CASE WHEN mt = 'qt' THEN rt END) as qty_sum,
STD(CASE WHEN mt = 'qt' THEN rt END) as qty_stddev,
SUM( mt = 'rel' ) as rel_count,
SUM(CASE WHEN mt = 'rel' THEN rel END) as rel_sum,
STD(CASE WHEN mt = 'rel' THEN rel END) as rel_stddev
FROM t_r
GROUP BY mid
) x;
请注意其他一些更改:
CASE
表达式.它使用MySQL扩展,将布尔值视为数字,其中1
为true,0
为false.ELSE
子句.聚合函数会忽略NULL
值,因此这应该可以解决您的问题.CASE
expression. This uses a MySQL extension that treats booleans as numbers with 1
for true and 0
for false.ELSE
clauses. Aggregation functions ignore NULL
values, so this should fix your problem.