MYSQL / SQL计数百分比查询

时间:2015-09-24 05:40:39

标签: php mysql json

我有什么

$query =
  "SELECT
      status_type,
      COUNT(*) as count,
      kpi_type,
      COUNT('kpi') * 100.00 as percentage
  FROM
      main
  GROUP BY
      status_type DESC;";

我得到了什么。

[{"Status":"SOLD","KPI_Percentage":"1400","KPI":"SALE","Status_Count":"14"}]

这是我的JSON数组

 $JSON_output[] = array('Status'         => $row['pin_status'],
                           'KPI'            => $row['kpi_type'],
                           'KPI_Percentage'         => $row['percent'],
                           'Status_Count'           => $row['count'],);}}

我想做什么......

[{"status":"value","kpi":"value","count":"12""percentage":"%32.42"}]

我正在尝试计算status_type,并显示status_type,我正在尝试显示kpi_type,并将kpi_type的百分比与其他kpi_type进行比较如果我有3个卖出,2个领先,5个潜在客户,它会显示:“LEAD:20%出售30%前景50%”

+------+-----------------+------------------+--------------+
| 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       |
+------+-----------------+------------------+--------------+

我正在寻找什么答案......

$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;";

如果您有任何问题,请与我们联系。

4 个答案:

答案 0 :(得分:2)

您可以尝试以下查询:

    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

添加评论。

答案 1 :(得分:1)

$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";

答案 2 :(得分:0)

我现在没有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`;

查询结果

        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]

答案 3 :(得分:0)

在修补Raveenanigam的答案后能够得到答案。感谢大家的帮助和赞成,以获得一些关注。

$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";

结果给出。

[{"status_type":"Hot Lead","KPI":"Lead","Percentage":"0.31596","Count":"2"}