计算SQLITE中单个记录的百分比

时间:2015-02-17 04:14:35

标签: android sqlite aggregate-functions percentage

我有Register表,在此我有以下字段:

r_id , f_name , l_name , isactive , pkg

pkg 字段的值可以是1,2,3,4。我想找出有多少用户有pkg值1和2以及3和4的百分比。 我使用了以下查询,但它对我不起作用。

SELECT COUNT(*) as cnt ,SUM(r_id) as sumSales ,AVG(r_id)*100 as avgSales
FROM reg where pkg=1

2 个答案:

答案 0 :(得分:1)

请尝试以下代码

SELECT pkg,ROUND((count(*)*100)/(SELECT count(*) FROM reg)) as percentage
FROM reg group by pkg

亲自尝试SqlFiddle

答案 1 :(得分:0)

如果我必须在网上这样做,我会这样做..

SELECT pkg,ROUND((count(*)/(SELECT count(*) FROM register))*100) as percentage FROM `register` group by pkg

希望它也适用于Android;)