[英]MYSQL / SQL Count Percentage Query
What I have 是)我有的
$query =
"SELECT
status_type,
COUNT(*) as count,
kpi_type,
COUNT('kpi') * 100.00 as percentage
FROM
main
GROUP BY
status_type DESC;";
What I am getting. 我得到了什么。
[{"Status":"SOLD","KPI_Percentage":"1400","KPI":"SALE","Status_Count":"14"}]
Here is my JSON array 这是我的JSON数组
$JSON_output[] = array('Status' => $row['pin_status'],
'KPI' => $row['kpi_type'],
'KPI_Percentage' => $row['percent'],
'Status_Count' => $row['count'],);}}
What I am trying to do... 我想做什么......
[{"status":"value","kpi":"value","count":"12""percentage":"%32.42"}]
I am trying to count status_type
, and show status_type
, and I am trying to show kpi_type
, and count % of kpi_type
compared to other kpi_type
s so If I have 3 sold, and 2 lead, and 5 prospects it would show: "LEAD: 20% SOLD 30% PROSPECTS 50%" 我正在尝试计算
status_type
,并显示status_type
,我正在尝试显示kpi_type
,并计算kpi_type
与其他kpi_type
s相比所以如果我有3个已售出,2个潜在客户和5个潜在客户,则会显示:“LEAD: 20%出售30%前景50%“
Table 表
+------+-----------------+------------------+--------------+
| ID | kpi_type | status_type | Is Deleted |
+------+-----------------+------------------+--------------+
| 1 | SALE |SOLD | no |
| 2 | LEAD |Maybe | no |
| 3 | LEAD |Hot Lead | no |
| 4 | PROSPECT |Not Home | no |
| 5 | SALE |SOLD | no |
| 6 | LEAD |Maybe | no |
| 7 | LEAD |Hot Lead | no |
| 8 | Not Interested |Not Interested | no |
+------+-----------------+------------------+--------------+
What answer I am seeking... 我正在寻求什么答案......
$query = //// THE CORRECT GOODS TO INSERT INTO THIS QUERY ////
"SELECT
status_type,
COUNT(*) as count,
kpi_type,
COUNT('kpi') * 100.00 as percentage
FROM
main
GROUP BY
status_type DESC;";
Let me know if you have any questions. 如果您有任何疑问,请告诉我。
You can try the below query: 您可以尝试以下查询:
SELECT status_type, kpi_type, Count(*),
((Count(*) * 100.0)/ (select Count(*) FROM main)) AS Percentage
FROM main
GROUP BY status_type, kpi_type
ORDER BY status_type, kpi_type
added comment. 补充评论。
$query = "SELECT status_type, kpi_type, Count(*) as count,((Count(*) * 100.0)/ (select Count(*) FROM main)) AS Percentage
FROM main
GROUP BY status_type, kpi_type
ORDER BY status_type, kpi_type";
I don't have MySql in front of me right now, but you need to get the total count of all records and then do a group by Status and KPI something like this (This is the rough syntax - maybe it'll get you closer): 我现在没有MySql在你面前,但你需要得到所有记录的总数,然后按状态和KPI做一组像这样(这是粗略的语法 - 也许它会让你更接近):
select @Total:=Count(*) from InputTable;
select `Status`, `Kpi`, Count(*) as Status_Count, ((Count(*) * 100.0)/ @Total) AS KPI_Percentage
from InputTable
group by `Status`, `Kpi`
order by `Status`, `KPI`;
Results from query 查询结果
Invalid query: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near 'select [status_type], [kpi_type], Count(), ((Count() * 100.0)/ @Total)
AS Perc' at line 3 Whole query: select @Total =(select Count() from main_pins)
select [status_type], [kpi_type], Count(), ((Count(*) * 100.0)/ @Total)
AS Percentage from main_pins group by [status_type], [kpi_type] order by [status_type],
[kpi_type]
Was able to get the answer after tinkering with Raveenanigam's answer. 在修补Raveenanigam的答案之后能够得到答案。 Thanks everyone for the help and the upvotes to get some attention.
感谢大家的帮助和赞成,以获得一些关注。
$query = "SELECT status_type, kpi_type, Count(*) as count,((Count(*) * 100.0)/ (select Count(*) FROM main)) AS Percentage
FROM main
GROUP BY status_type, kpi_type
ORDER BY status_type, kpi_type";
result given. 结果给出。
[{"status_type":"Hot Lead","KPI":"Lead","Percentage":"0.31596","Count":"2"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.