0

我希望返回按每个分组的百分比,例如根据它们分组的有id_cerinta多少subcerinteProiect录音被acoperire= 'acoperita'划分为所有录音subcerinteProiectid_sarcina

在此处输入图像描述

我尝试了类似的东西:

SELECT 
    id_cerinta,
    SUM(CASE WHEN acoperire = 'acoperita' THEN 1 ELSE 0 END) 
        / COUNT(*) * 100 AS nr_subcerinte_acoperit 
FROM 
    subcerinteProiect 
GROUP BY 
    id_cerinta

但是效果不太好。它只返回 0..

4

1 回答 1

3

这是由于整数数学。试试这些:

SELECT 5/100, 57/100, 99/100;

结果都是0。

现在尝试:

SELECT 5.0/100, 57.0/100, 99.0/100;

更好的结果,对吧?

因此,对于您的查询:

SELECT 
    id_cerinta, 
    CONVERT(DECIMAL(5,2), 
        SUM(CASE WHEN acoperire = 'acoperita' THEN 1 ELSE 0 END) 
            / COUNT(*) * 100.0) AS nr_subcerinte_acoperit 
FROM 
    subcerinteProiect 
GROUP BY
    id_cerinta
于 2012-07-12T15:55:02.807 回答