我有以下查询:
SELECT SUM(KDV_Haric_Ciro) AS Ciro
, Ana_Kategori
FROM general_Table
Where EczaneID = $pharmacy_id
AND Yil = '$dateYear'
GROUP
BY Ana_Kategori
ORDER
BY Ciro DESC
但我希望结果是百分比。
目前结果如下:
A 5
B 7
C 8
我尝试过使用百分比方法的嵌套查询,但都失败了。
答案 0 :(得分:0)
要获得百分比,您需要总数。为此,您需要另一个计算。一种方法是:
SELECT gt.Ana_Kategori, SUM(gt.KDV_Haric_Ciro) AS Ciro,
SUM(gt.KDV_Haric_Ciro) / total.KDV_Haric_Ciro as ratio
FROM general_Table gt CROSS JOIN
(SELECT SUM(gt2.KDV_Haric_Ciro) as KDV_Haric_Ciro
FROM general_Table gt2
WHERE gt2.EczaneID = '".$pharmacy_id."' AND gt2.Yil = '".$dateYear."'
) total
WHERE gt.EczaneID = '".$pharmacy_id."' AND gt.Yil = '".$dateYear."'
GROUP BY gt.Ana_Kategori
ORDER BY `Ciro` DESC;
注意:您不应该使用参数值来设置查询字符串。您应该学习如何编写参数化查询。